2010-06-03 57 views
0

以下功能將文件從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) 
+0

「權限相關錯誤」太含糊。發佈堆棧跟蹤和異常消息。 – 2010-06-03 17:54:04

回答

0

如果要移動的文件中,要求每個Vista用戶帳戶控制(UAC)管理權限的地方,你將需要運行應用程序管理員,在右鍵菜單從下面:

alt text http://www.trendystock.com/images/Run%20as%20administrator%20for%20Vista%20PC.jpg


如果這很麻煩,用戶可以右鍵單擊快捷方式或.exe,然後轉到屬性,您可以選擇以管理員身份始終運行該應用程序。或者,如果它是一個選項,他們可以禁用UAC。

+0

這已經完成;用戶確實點擊以管理員身份運行,並且我的應用在其清單中具有「asadministrator」。 – 2010-06-03 21:02:16