Win32Exception:目录名称无效
我正在尝试以不同的用户身份运行一个进程,该用户在两台运行 Vista 且启用了 UAC 的不同计算机中具有管理员权限,但在其中一台计算机中,我收到一个 Win32Exception,显示“目录名称无效”
谁能告诉我什么是我的代码有问题吗?
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);
更新
执行上述代码的应用程序具有 requireAdministrator 执行级别。 我什至将工作文件夹设置为 "Path.GetDirectoryName(myFile)" 和 "New System.IO.FileInfo(myFile).DirectoryName"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我也有过类似的经历,结果证明是我们的开发环境的问题。 我们使用 subst 命令将源代码目录映射到虚拟驱动器。 文件名和工作目录属性时,文件名和工作目录属性被设置为“W:\SomeFolder\FileName.exe”。
因此,当我硬编码 FileName & WorkDirectory 通过我的实际磁盘 (C:) 访问文件,我停止收到“无效目录”异常。
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.
这是由于文件夹名称中存在空格造成的。 一旦我删除了空间,当我遇到这个问题时,它就开始工作文件。
It is due to space in the folder name. Once I removed the space it started working file when I hit this issue.
这是因为文件的路径长度超过255个字符。
It is because the path length of the file exceeds 255 characters.
尝试替换
为
FileInfo 可以访问文件系统,我假设只有管理员用户有权访问该目录。 如果它不能解决你的问题,至少它会让你的代码更快一点......
Try to replace
with
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...
您需要指定 ProcessStartInfo` 的
WorkingDirectory
属性。 来自Win32Exception错误代码267“目录名称无效”< /a>:You need to specify the
WorkingDirectory
property of ProcessStartInfo`. From Win32Exception error code 267 "The directory name is invalid":该目录是登录用户的映射主文件夹还是低于该目录? 这篇知识库文章可能会有所帮助:
更新中的功能:请注意,在 Vista 上,成为本地管理员组的成员和拥有管理权限是不同的。
我认为当您以管理员身份运行 C# 应用程序时,一切正常。 右键单击可执行文件,然后选择以管理员身份运行,或从提升的命令提示符启动应用程序(获取该应用程序的最快方法是按开始,输入“cmd” ' 后跟
Ctrl+Shift+Return
)。或者,作为替代方案,为运行该进程的帐户禁用 UAC。
Is the directory the logged-on user's mapped home folder or below that? Than this knowledge base article might help:
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.