Process调用DOS相关问题

dongshanyu dongshanyu 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
想通过Process调用DOS命令连接目标机器上的共享文件夹,代码如下:

Dim objProc As New System.Diagnostics.Process
Try
objProc.StartInfo.FileName = "CMD.EXE"
objProc.StartInfo.UseShellExecute = False
objProc.StartInfo.RedirectStandardInput = True
objProc.StartInfo.RedirectStandardOutput = True
objProc.StartInfo.RedirectStandardError = True
objProc.StartInfo.CreateNoWindow = True
objProc.Start()
Dim strCmd As String = String.Empty
'strCmd = String.Format("NET USE {0} ""{1}"" /USER:""{2}"" ", strDir, strPWD, strUser)
strCmd = String.Format("NET USE {0} ""{1} /USER:{2}"" ", strDir strPWD, strUser)
objProc.StandardInput.WriteLine(strCmd)
objProc.StandardInput.WriteLine("EXIT")
While Not objProc.HasExited
objProc.WaitForExit(1000)
End While
Dim strErrorMsg As String = objProc.StandardError.ReadToEnd
objProc.StandardError.Close()
If strErrorMsg Is Nothing Or strErrorMsg = String.Empty Then
Else
Throw New Exception(strErrorMsg)
End If
Catch ex As Exception
Return False
Finally
objProc.Close()
objProc.Dispose()
End Try

但连接失败,系统提示:
System error 1326 has occurred.
Logon failure: unknown user name or bad password.

但若通过手工cmd窗口,执行上面的 NET USE,连接没问题。
有谁知道问题在哪?

...全文
评论 6
收藏 已收藏
分享
返回顶部
dongshanyu
.NET社区 62,272 成员 668,968 内容