VB.net将文件夹从SharePoint复制到本地计算机

时间:2013-02-24 23:50:15

标签: vb.net sharepoint directoryinfo

我编写了一个VB.net应用程序来备份我们的SharePoint 2007服务器上的文件夹,并在其位置放置文件的新副本。这个应用程序在我的Windows 7计算机上运行完美,但是当一位同事在他们的Windows XP计算机上运行它时会出现一些错误。尽我所能解决它们,我不能。

第一个错误是当我调暗DirectoryInfo变量时。它连接到SharePoint服务器上的WebDav文件夹。似乎除非用户首先手动将驱动器映射到UNC(使用他们的常规登录,没有特殊凭据),否则他们会收到“拒绝访问”错误。由于我有一个解决方法,这并不重要,但如果有解决方案,它会有所帮助。

第二个(也是最重要的)错误是在复制文件期间Windows XP计算机出现“延迟写入失败”错误。收到此错误的那些文件未正确备份。此错误甚至不会阻止程序运行!有办法解决这个问题吗?

Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click

    ' Dim all variables
    Dim i, countfiles As Integer
    Dim CompletedReports As New DirectoryInfo(GlobalVariables.ServerURL)
    Dim dNow As Date = Date.Today
    Dim friday As Date
    Dim friD, friM, friY, archiveFolder, archiveURL As String
    Dim FreshReports As New DirectoryInfo(GlobalVariables.ServerURL + "\Report Archive\Templates\")

    ' Prevent form from being closed while running
    btnDone.Enabled = False
    btnGo.Enabled = False
    btnRunSetup.Enabled = False
    Application.DoEvents()

    ' Calculate the archive date
    friday = dNow.AddDays(-(CInt((dNow.DayOfWeek + 1) Mod 7) + 1))
    friD = friday.Day
    friM = friday.Month
    friY = friday.Year
    If Len(friD) < 2 Then
        friD = "0" + friD
    End If
    If Len(friM) < 2 Then
        friM = "0" + friM
    End If
    archiveFolder = friY + "-" + friM + "-" + friD

    archiveURL = GlobalVariables.ServerURL + "\Report Archive\" + archiveFolder

    ' Count number of files in the current folder
    RecursiveCount(CompletedReports, i)

    ' Do the SharePoint Archive to the "Report Archive" folder
    countfiles = i
    PB_SharePointArchive.Maximum = i
    PB_SharePointArchive.Step = 1
    CopyDirectory(GlobalVariables.ServerURL, archiveURL, PB_SharePointArchive, LB_SharePointArchive_Name, False)
    LB_SharePointArchive_Name.Text = "Done!"

    ' If specified, do the Server Backup
    If GlobalVariables.RemoteBackup = True Then
        i = countfiles
        PB_RemoteBackup.Maximum = i
        PB_RemoteBackup.Step = 1
        CopyDirectory(GlobalVariables.ServerURL, GlobalVariables.RemotePath + archiveFolder, PB_RemoteBackup, LB_RemoteBackup_Name, False)
        LB_RemoteBackup_Name.Text = "Done!"
    End If

    ' If specified, do the Local Backup
    If GlobalVariables.LocalBackup = True Then
        i = countfiles
        PB_LocalBackup.Maximum = i
        PB_LocalBackup.Step = 1
        CopyDirectory(GlobalVariables.ServerURL, GlobalVariables.LocalPath + archiveFolder, PB_LocalBackup, LB_LocalBackup_Name, False)
        LB_LocalBackup_Name.Text = "Done!"
    End If

    ' Refresh the Reports
    i = 0
    RecursiveCount(FreshReports, i)
    PB_SharePointRefresh.Maximum = i
    PB_SharePointRefresh.Step = 1
    CopyDirectory(GlobalVariables.ServerURL + "\Report Archive\Templates\", GlobalVariables.ServerURL, PB_SharePointRefresh, LB_SharePointRefresh_Name, True)
    LB_SharePointRefresh_Name.Text = "Done!"

    ' All tasks done - unlock the close function
    btnDone.Enabled = True

End Sub


Private Sub CopyDirectory(ByVal sourceDir As String, ByVal destDir As String, ByRef progBar As ProgressBar, ByRef statusBox As Label, ByVal overwrite As Boolean)

    Dim prompt, retry

    If Not Directory.Exists(destDir) Then
        Directory.CreateDirectory(destDir)
    ElseIf Directory.Exists(destDir) And overwrite = False Then
        'MsgBox("The directory already exists!" + vbCrLf + "It looks like the backup has already been done for this week." + vbCrLf + "To continue, please delete the previous backup and restart the process", MsgBoxStyle.Critical, "TSO Report Refresh")

        prompt = MsgBox("The directory already exists!" + vbCrLf + "It looks like the backup has already been done for this week." + vbCrLf + "Do you wish to delete the previous backup?", MsgBoxStyle.YesNo, "TSO Report Refresh")
        If prompt = vbYes Then
            DeleteDirectory(destDir)
            Directory.CreateDirectory(destDir)
        Else
            MsgBox("You have chosen not to delete the previous backup. Backup cannot continue." + vbCrLf + "To continue, please delete the previous backup and restart the process", MsgBoxStyle.Critical, "TSO Report Refresh")
        End If
    End If

    For Each strEntry As String In Directory.GetFiles(sourceDir)
        Dim fileNew As FileInfo
        fileNew = New FileInfo(strEntry)

        If fileNew.Exists Then
            retry = 3
            statusBox.Text = "Current File: " + fileNew.Name
            Application.DoEvents()
            While retry > 0
                Try
                    fileNew.CopyTo(destDir & "\" & fileNew.Name, True)
                    retry = 0
                    progBar.PerformStep()
                    Application.DoEvents()
                Catch ex As Exception
                    retry = retry - 1
                    If retry = 0 Then
                        MsgBox("A file has failed to copy after three attempts. The application will now close." + vbCrLf + fileNew.Name, MsgBoxStyle.Critical, "TSO Report Refresh")
                        End
                    End If
                    'Debug.Print("Retrying file " + fileNew.Name + " for the " + (3 - retry) + " time...")
                End Try
            End While
        End If
    Next

    For Each strEntry As String In Directory.GetDirectories(sourceDir)
        If Path.GetFileName(strEntry) = "Report Archive" Or Path.GetFileName(strEntry) = "Forms" Then
            'Do Nothing!
        Else
            Dim strDest As String = Path.Combine(destDir, Path.GetFileName(strEntry))
            CopyDirectory(strEntry, strDest, progBar, statusBox, overwrite)
        End If
    Next

End Sub

0 个答案:

没有答案