Win32Exception: The directory name is invalid
Asked Answered
R

6

15

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"

Rashidarashidi answered 13/6, 2009 at 11:50 Comment(5)
In what line of code does the error occur? Is it when you create the FileInfo object or when you start the process?Wallie
@divo System.Diagnostics.Process.Start(pInfo)Rashidarashidi
You can use Process Monitor (from Sysinternals) to determine which underlying Win32 file/folder operation is failing.Savoyard
By the way, you can use a string literal to avoid having to escape slashes in paths, for example, var myFile = @"D:\SomeFolder\MyExecutable.exe";.Nuke
If your SomeFolder path contains Program Files (x86) folder, Visual studio or some other tool mystically removes a space between 's' and '(' and make it "Program Files(x86)". Maybe you missed this typoLauderdale
R
9

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

Rashidarashidi answered 12/4, 2010 at 12:20 Comment(1)
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'.Fogg
T
19

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. ...

Tideland answered 1/8, 2014 at 4:41 Comment(1)
I add string like same directory name and not working. but after I change like this answer FileInfo fileInfo = new FileInfo(path); startInfo.WorkingDirectory = fileInfo.DirectoryName; working fine. weird.Excurrent
R
9

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

Rashidarashidi answered 12/4, 2010 at 12:20 Comment(1)
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'.Fogg
C
4

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...

Cozy answered 13/6, 2009 at 13:16 Comment(1)
Doesn't solve the problem. I even specified a static address.Rashidarashidi
W
3

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.

Wallie answered 13/6, 2009 at 12:47 Comment(3)
The Directory is a normal directory in a non-system drive that has Full Control permission for Administrators group.Rashidarashidi
The application that runs the second process has requireAdministrator execution level. Therefor it will run as administrator.Rashidarashidi
"...disable UAC for the account running that process" - when you start doing that, its near impossible to go back. Its like crossing over to the dark side.Berga
P
2

It is due to space in the folder name. Once I removed the space it started working file when I hit this issue.

Polyhedron answered 8/12, 2010 at 21:36 Comment(1)
This doesn't work even if we dont have space in the pathGules
M
2

I had a similar experience and it turned out to be an issue with our development environment. We map our source code directory to a virtual drive using the subst command. So the FileName and WorkingDirectory properties were being set to "W:\SomeFolder\FileName.exe"

When I hard-coded the FileName & WorkingDirectory to access the files via my actual disk (C:), I stopped receiving the "Invalid Directory" exception.

Mayweed answered 9/8, 2011 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.