我有一个用 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()
这与我在映射驱动器上使用的实现相同,但是使用 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)