【发布时间】:2017-12-28 09:07:51
【问题描述】:
我看到很多类似的问题,但我仍然卡住了。我可以从 cmd 提示符运行 nltest.exe,但不能以编程方式运行。
这里是失败的代码,出现异常“System.dll 中发生'System.ComponentModel.Win32Exception' 类型的未处理异常”
Dim pz As New Process()
pz.StartInfo.FileName = "nltest.exe"
pz.StartInfo.Arguments = " /dsgetsite > c:\temp\where.txt"
pz.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
pz.Start()
所以,我修改为使用 cmd /c 如下:
Dim pz As New Process()
pz.StartInfo.FileName = "cmd"
pz.StartInfo.Arguments = " /c nltest /dsgetsite > c:\temp\where.txt"
' pz.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
pz.Start()
pz.WaitForExit()
使用 cmd /c,代码运行但只创建一个空文件 c:\temp\where.txt。
如果我从“开始”菜单 (Windows 10) 手动运行该命令,则该命令运行良好,并且该文件包含站点位置。
我是否在这两种尝试中都犯了基本错误?
【问题讨论】:
-
你需要指定它在哪里。
-
把/k里的/c改一下,看看报错信息
-
如果同一命令行在控制台执行时有效,但在通过
Process.Start调用时无效,这通常是因为正在执行的应用程序假定其工作目录的位置。Process.Start默认使用您自己的应用程序的工作目录,因此假设它的工作目录是包含其自己的 EXE 的文件夹的应用程序将无法在那里找到文件。可以将ProcessStartInfo的WorkingDirectory设置为包含“nltest.exe”的文件夹的路径吗? -
添加 pz.StartInfo.WorkingDirectory = "c:\Windows\System32" 没有区别
-
添加
/k会打开 cmd 窗口并显示错误:'nltest' is not recognized as an internal or external command, operable program or batch file.谷歌搜索让我相信我可能需要以管理员身份运行它。