【问题标题】:Exception - System cannot find the file specified异常 - 系统找不到指定的文件
【发布时间】: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. 谷歌搜索让我相信我可能需要以管理员身份运行它。

标签: vb.net process


【解决方案1】:

为了使用活动目录功能dsgetsite,我放弃了命令行功能nltest,并基于netapi32.dll翻译了http://adcoding.com/using-dsgetsitename-in-c-sample-how-to-get-the-name-of-the-site-where-a-computer-resides/找到的c#(使用converter.telerik.com)。

我将此行添加到我的 vb.net 代码模块的 IMPORTS 部分

Imports System.Runtime.InteropServices

然后我添加到主模块

  <DllImport("netapi32.dll", CharSet:=CharSet.Auto)>
  Public Function DsGetSiteName(ByVal ComputerName As String, <Out> ByRef SiteName As IntPtr) As Integer
  End Function

  Public Function GetLocation()
       Dim pSiteInfo As IntPtr
       Dim sSiteName As String = ""
        If DsGetSiteName(String.Empty, pSiteInfo) = 0 Then
              sSiteName = Marshal.PtrToStringAuto(pSiteInfo)
        End If
       Return sSiteName 
  End Function

当我想从工作站中找到工作站的站点名称时,我调用函数 GetLocation()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    相关资源
    最近更新 更多