Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win32Exception: The directory name is invalid

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"

like image 601
mrtaikandi Avatar asked Jun 13 '09 11:06

mrtaikandi


People also ask

Why does it say the directory name is invalid?

One of the main and common reasons behind the occurrence of error "the directory name is invalid" can be the disk errors. These disk errors can be a result of various things such as data corruption, incorrect formatting, heating of the disk, damage, etc.


1 Answers

You need to specify the WorkingDirectory property of ProcessStartInfo`. From Win32Exception error code 267 "The directory name is invalid":

I'm currently working on an "Automated Run As" tool. Its goal is helping admins which, like me, have to give users a means to execute one or two programs as Administrator and would like to do so without having to surrender an admin's password.

So, I'm developing on Vista and I just whipped up a small proof of concept prototype, that'd run calc.exe as a different user, using ProcessStartInfo and Process. This worked fine when I executed it as myself (a rather pointless exercise, I must admit), but when I created a new user and tried to run it as him, I stumbled upon a Win32Exception complaining that the directory name is invalid, native error code 267. I was instsantly baffled, as I knew of no supplied directory name that could be invalid. I then tested the code on an XP machine and it worked!

I started googling on it to no avail, many reports of that error but no conclusive solution, or on different contexts. Finally, after a while it dawned on me, I wasn't specifying the WorkingDirectory property of the ProcessStartInfo class, as soon as I added the lines

FileInfo fileInfo = new FileInfo(path); startInfo.WorkingDirectory = fileInfo.DirectoryName;

To my code, it was allowed to run code as different than logged in user. ...

like image 174
Chandler Avatar answered Sep 20 '22 03:09

Chandler