TJO*_*OHN 3 .net c# vb.net process.start
我有一个我在VB.net中编写的实用程序,它作为计划任务运行.它在内部调用另一个可执行文件,它必须访问映射的驱动器.显然,当用户未登录时,即使将身份验证凭据提供给任务本身,Windows也会遇到计划任务访问映射驱动器的问题.好的.
为了解决这个问题,我刚刚通过了我的应用程序UNC路径.
process.StartInfo.FileName = 'name of executable'
process.StartInfo.WorkingDirectory = '\\unc path\'
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
process.StartInfo.Arguments = 'arguments to executable'
process.Start()
Run Code Online (Sandbox Code Playgroud)
这与我使用映射驱动器的实现相同,但是使用UNC路径时,该过程的行为不像UNC路径是工作目录.
将ProcessStartInfo.WorkingDirectory设置为UNC路径是否存在任何已知问题?如果没有,我做错了什么?
用户未登录时,映射驱动器的问题是它们不存在.驱动器仅映射并可供当前登录的用户使用.如果没有人登录,则不会映射任何驱动器.
作为一种解决方法,您可以运行CMD,调用PUSHD,它将您的UNC映射到幕后的驱动器,然后执行您的代码.我已经从我的system32复制了tree.com文件并将其作为"tree4.com"放在我的文件服务器上,这段代码按预期工作(我也重定向标准输出,所以我可以看到调用的结果,但那是不必要)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using P As New Process()
'Launch a standard hidden command window
P.StartInfo.FileName = "cmd.exe"
P.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
P.StartInfo.CreateNoWindow = True
'Needed to redirect standard error/output/input
P.StartInfo.UseShellExecute = False
P.StartInfo.RedirectStandardInput = True
P.StartInfo.RedirectStandardOutput = True
'Add handler for when data is received
AddHandler P.OutputDataReceived, AddressOf SDR
'Start the process
P.Start()
'Begin async data reading
P.BeginOutputReadLine()
'"Map" our drive
P.StandardInput.WriteLine("pushd \\file-server\File-Server")
'Call our command, you could pass args here if you wanted
P.StandardInput.WriteLine("tree2.com c:\3ea7025b247d0dfb7731a50bf2632f")
'Once our command is done CMD.EXE will still be sitting around so manually exit
P.StandardInput.WriteLine("exit")
P.WaitForExit()
End Using
Me.Close()
End Sub
Private Sub SDR(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
Trace.WriteLine(e.Data)
End Sub
Run Code Online (Sandbox Code Playgroud)