【问题标题】:Vista seems to prevent .net from reading/updating file attributesVista 似乎阻止 .net 读取/更新文件属性
【发布时间】: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


【解决方案1】:

如果要移动的文件位于需要根据 Vista 用户帐户控制 (UAC) 管理权限的位置,则您需要以管理员身份运行应用程序,如下面的右键菜单中所示:

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


如果这很麻烦,用户可以右键单击快捷方式或 .exe,然后转到可以选择始终以管理员身份运行应用程序的属性。或者,如果可以选择,他们可以禁用 UAC。

【讨论】:

  • 这已经完成了;用户确实单击了以管理员身份运行,并且我的应用在其清单中有“管理员身份”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-08
  • 2010-12-17
相关资源
最近更新 更多