C# Win32Exception: 目录名无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/990562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:03:29  来源:igfitidea点击:

Win32Exception: The directory name is invalid

c#processstartinfowin32exception

提问by mrtaikandi

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"

我正在尝试以不同的用户身份运行一个进程,该用户在 2 台运行 Vista 且启用了 UAC 的不同计算机上具有管理员权限,但在其中一台计算机中,我收到一个 Win32Exception,显示“目录名称无效”

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"

执行上述代码的应用程序具有 requireAdministrator 执行级别。我什至将工作文件夹设置为“Path.GetDirectoryName(myFile)”“New System.IO.FileInfo(myFile).DirectoryName”

采纳答案by mrtaikandi

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

这是因为文件的路径长度超过了 255 个字符。

回答by Dirk Vollmar

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

使用 Windows 中的运行方式功能启动 Cmd.exe 或 Notepad.exe 时出现“目录名称无效”错误消息

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

更新:请注意,作为本地管理员组的成员和拥有管理权限在 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).

我想当您以管理员身份运行 C# 应用程序时,一切正常。右键单击可执行文件,然后选择以管理员身份运行,或从提升的命令提示符启动应用程序(获取命令的最快方法是按Start,输入“cmd”,然后输入Ctrl+Shift+Return)。

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

或者,作为替代方案,为运行该进程的帐户禁用 UAC。

回答by chris166

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

FileInfo 可以访问文件系统,我假设只有 admin 用户有权访问该目录。如果它不能解决您的问题,至少它会使您的代码更快一点......

回答by Alto

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

这是由于文件夹名称中有空格。一旦我删除了空间,当我遇到这个问题时它就会开始工作文件。

回答by Walt D.

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"

我有过类似的经历,结果证明是我们的开发环境有问题。我们使用 subst 命令将源代码目录映射到虚拟驱动器。所以 FileName 和 WorkingDirectory 属性被设置为“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.

当我对 FileName & WorkingDirectory 进行硬编码以通过我的实际磁盘 (C:) 访问文件时,我不再收到“无效目录”异常。

回答by Chandler

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

您需要指定WorkingDirectoryProcessStartInfo`的属性。从Win32Exception 错误代码 267“目录名称无效”

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

我目前正在研究“自动运行方式”工具。它的目标是帮助管理员,像我一样,必须为用户提供一种以管理员身份执行一两个程序的方法,并且希望在不必交出管理员密码的情况下这样做。

所以,我正在 Vista 上开发,我只是创建了一个概念原型的小证明,它会以不同的用户身份运行 calc.exe,使用 ProcessStartInfo 和 Process。当我自己执行它时,这很好用(我必须承认这是一个相当无意义的练习),但是当我创建一个新用户并尝试以他的身份运行它时,我偶然发现了一个 Win32Exception 抱怨目录名无效,本机错误代码 267。我立刻感到困惑,因为我知道没有提供可能无效的目录名称。然后我在 XP 机器上测试了代码并且它工作了!

我开始在谷歌上搜索它无济于事,许多关于该错误的报告但没有确定的解决方案,或者在不同的上下文中。最后,过了一会儿我才明白,我没有指定 ProcessStartInfo 类的 WorkingDirectory 属性,只要我添加了这些行

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

对于我的代码,允许以与登录用户不同的方式运行代码。...