【发布时间】:2011-02-27 10:15:08
【问题描述】:
以下函数将文件从Source & Path复制到Dest & Path,通常在复制前将文件属性设置为正常。
然而,我的应用程序的用户在复制只读文件时报告它失败,返回与权限相关的错误。然而,用户以管理员身份运行代码,并且错误发生 - 非常奇怪 - 在 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)
【问题讨论】:
-
“权限相关错误”太模糊了。发布异常的堆栈跟踪和消息。
标签: vb.net file-io file-permissions