以下功能將文件從Source & Path
複製到Dest & Path
,通常在複製之前將文件屬性設置爲正常。Vista似乎可以防止.net讀取/更新文件屬性
但是,我的應用程序的用戶在複製只讀文件時報告它失敗,並返回與權限相關的錯誤。然而,用戶以管理員的身份運行代碼,並且在SetLastWriteTimeUtc
行上發生錯誤 - 非常奇怪。
儘管代碼報告文件屬性設置爲正常,但Windows資源管理器顯示它們已設置爲只讀。
Sub CopyFile(ByVal Path As String, ByVal Source As String, ByVal Dest As String)
If IO.File.Exists(Dest & Path) Then IO.File.SetAttributes(Dest & Path, IO.FileAttributes.Normal)
IO.File.Copy(Source & Path, Dest & Path, True)
If Handler.GetSetting(ConfigOptions.TimeOffset, "0") <> "0" Then
IO.File.SetAttributes(Dest & Path, IO.FileAttributes.Normal)
IO.File.SetLastWriteTimeUtc(Dest & Path, IO.File.GetLastWriteTimeUtc(Dest & Path).AddHours(Handler.GetSetting(ConfigOptions.TimeOffset, "0")))
End If
IO.File.SetAttributes(Dest & Path, IO.File.GetAttributes(Source & Path))
End Sub
我完全感覺不到這個代碼的問題,所以很長時間尋找的解決方案後,我想到了SO VB.Net大師可能幫助:)
感謝了很多之一。
編輯:
實際的錯誤是
Access to the path '(..)' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.IO.File.OpenFile(String path, FileAccess access, SafeFileHandle& handle)
at System.IO.File.SetLastWriteTimeUtc(String path, DateTime lastWriteTimeUtc)
「權限相關錯誤」太含糊。發佈堆棧跟蹤和異常消息。 – 2010-06-03 17:54:04