[英]Exception - System cannot find the file specified
我看到許多與此類似的問題,但我仍然堅持。 我可以從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)手動運行命令,則此命令運行良好,並且該文件包含站點位置。
我在這兩種嘗試中都犯了基本錯誤嗎?
為了使用活動目錄功能dsgetsite,我放棄了命令行功能nltest並翻譯了http://adcoding.com/using-dsgetsitename-in-c-sample-how中找到的c#(使用converter.telerik.com ) 獲取基於netapi32.dll 的計算機所在/站點的名稱。
我將此行添加到我的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()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.