代码之家  ›  专栏  ›  技术社区  ›  Clément

Vista似乎阻止.net读取/更新文件属性

  •  0
  • Clément  · 技术社区  · 16 年前

    下面的函数从 Source & Path Dest & Path ,通常在复制之前将文件属性设置为“正常”。

    然而,我的应用程序的一个用户报告它在复制只读文件时失败,返回一个与权限相关的错误。但是,用户是 以管理员身份运行代码 很奇怪,错误发生在 SetLastWriteTimeUtc 线路。
    尽管代码报告文件属性设置为normal,但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
    

    谢谢。

    编辑 :

    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)
    
    1 回复  |  直到 16 年前
        1
  •  0
  •   Ryan Hayes    16 年前

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

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


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