views:

1135

answers:

4

I'm trying to run a process as a different user that has Administrator privilege in 2 different computers running Vista and their UAC enabled but in one of them I get a Win32Exception that says "The directory name is invalid"

Can anyone tell me what is wrong with my code?

var myFile = "D:\\SomeFolder\\MyExecutable.exe";
var workingFolder = "D:\\SomeFolder";
var pInfo = new System.Diagnostics.ProcessStartInfo();
pInfo.FileName = myFile;
pInfo.WorkingDirectory = workingFolder;
pInfo.Arguments = myArgs;
pInfo.LoadUserProfile = true;
pInfo.UseShellExecute = false;
pInfo.UserName = {UserAccount};
pInfo.Password = {SecureStringPassword};
pInfo.Domain = ".";

System.Diagnostics.Process.Start(pInfo);

UPDATE

The application that executes the above code has requireAdministrator execution level. I even set the working folder to "Path.GetDirectoryName(myFile)" and "New System.IO.FileInfo(myFile).DirectoryName"

+1  A: 

Is the directory the logged-on user's mapped home folder or below that? Than this knowledge base article might help:

"The directory name is invalid" error message when you start Cmd.exe or Notepad.exe by using the Run as feature in Windows

Update: Please note that being member of the Local Administrators group and having administrative privileges are not the same on Vista.

I suppose that everything works fine when you run your C# application as administrator. Right-click the executable, then choose Run as Administrator, or start the application from an elevated command prompt (the fastest way to get one is by pressing Start, enter 'cmd' followed by Ctrl+Shift+Return).

Or, as an alternative, disable UAC for the account running that process.

0xA3
The Directory is a normal directory in a non-system drive that has Full Control permission for Administrators group.
Mohammadreza
The application that runs the second process has requireAdministrator execution level. Therefor it will run as administrator.
Mohammadreza
A: 

Try to replace

pInfo.WorkingDirectory = New System.IO.FileInfo(myFile).DirectoryName;

with

pInfo.WorkingDirectory = Path.GetDirectoryName(myFile);

The FileInfo makes an access to the filesystem, and I would assume only the admin user has access to that directory. If it doesn't solve your problem, at least it will make your code a tiny bit faster...

chris166
Doesn't solve the problem. I even specified a static address.
Mohammadreza
A: 

I also face the problem. Please help.

sheeba
This is not a forum. If anywhere this should be a comment below the question, though in reality it is probably not necessary
David Archer
+1  A: 

It is because the path length of the file exceeds 255 characters.

Mohammadreza
Thank you, this worked for me too, I had my entire path and file name set in the filename attribute, instead, place your path under the 'Working Directory' property, leave the filename property just for the 'file name'.
Dal