Question

I have a program I'm building in C#. It copies a file from a network drive to your desktop.

string desktop = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
File.Copy("T:\\DATS Launcher.exe", desktop + "\\DATS Launcher.exe", true);

If I run the program normally, it works.

If I run the program with "Run as Administrator", I get:

************** Exception Text **************

System.IO.DirectoryNotFoundException: Could not find a part of the path 'T:\DATS Launcher.exe'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

Why might this be occurring?

Was it helpful?

Solution

The T: drive isn't mapped when you're running as an administrator, since it's running as a different user.

So, you should use the UNC path of the T: drive, rather than the drive name.

OTHER TIPS

T: seems to be a network drive only mounted for the current user.

You can also do the following:

Set string Letter="T"; and string Path=@"\\server\share"; (except use your server and share...)

Then

ProcessStartInfo psi=new ProcessStartInfo(
    "net.exe",
    "use "+Letter+" \""+Path+"\" /persistent:yes");
psi.CreateNoWindow=true; // We don't need a console showing up for this
psi.UseShellExecute=false; // Most likely optional. Required only if you want to
    // mess with the standard input/output of the process.
    // (for example, to check if mapping was successful).

Process prc=Process.Start(psi);

You may also want to set /persistent:no if it's a single-ish use application, but use your judgement on that.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top