我编写了一个 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