views:

28

answers:

1

Hello everyone!

The following function copies a file from Source & Path to Dest & Path, normally setting file attributes to normal before copying.

Yet, a user of my app has reported it to fail when copying readonly files, returning a permissions-related error. The user is however running the code as administrator, and the error happens - quite strangely - on the SetLastWriteTimeUtc line.
Although the code reports that the file attributes are set to normal, windows explorer shows that they are set to read only.

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

I just fail to see the problem in this code, so after long hours of searching for the solution, I thought one of SO VB.Net Gurus might help :)

Thanks a lot.

Edit:
The actual error is

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)
A: 

If the files being moved are in a place that requires administration permissions per the Vista User Account Controls (UAC), you will need to run the application as administrator, as in the right click menu from below:

alt text


If this is a hassle, the user can right click the shortcut or .exe, and go to properties where you can select to always run the app as administrator. Alternatively, if it's an option, they could just disable UAC.

Ryan Hayes
This is already being done ; the user does click on run as administrator, and my app has "asadministrator" in its manifest.
CFP