Win32Exception:目录名称无效

发布于 2024-07-23 22:33:10 字数 799 浏览 13 评论 0原文

我正在尝试以不同的用户身份运行一个进程,该用户在两台运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

简单 2024-07-30 22:33:12

我也有过类似的经历,结果证明是我们的开发环境的问题。 我们使用 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.

寂寞美少年 2024-07-30 22:33:12

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

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

千纸鹤带着心事 2024-07-30 22:33:11

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

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

旧街凉风 2024-07-30 22:33:11

尝试替换

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

pInfo.WorkingDirectory = Path.GetDirectoryName(myFile);

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

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

独孤求败 2024-07-30 22:33:11

您需要指定 ProcessStartInfo` 的 WorkingDirectory 属性。 来自Win32Exception错误代码267“目录名称无效”< /a>:

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

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

我开始用谷歌搜索它,但没有结果,有很多关于该错误的报告,但是
没有结论性的解决方案,或者在不同的背景下。 最后,经过一段
当我意识到时,我没有指定工作目录
当我添加这些行后,ProcessStartInfo 类的属性

FileInfo fileInfo = new FileInfo(路径); 开始信息.工作目录 =
fileInfo.DirectoryName;

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

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

骄傲 2024-07-30 22:33:11

该目录是登录用户的映射主文件夹还是低于该目录? 这篇知识库文章可能会有所帮助:

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

更新中的功能:请注意,在 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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文