设置ProcessStartInfo.WorkingDirectory一个UNC路径路径、ProcessStartInfo、WorkingDirectory、UNC

2023-09-05 23:09:22 作者:国产祖宗

我有一个计划任务运行,我已经写在VB.net的工具。它在内部调用另一个可执行文件,它具有访问映射驱动器。显然,窗户与计划任务访问映射的驱动器时,用户没有登录上,即使身份验证凭据提供给任务本身的问题。好了,就好了。

I have a utility that I have written in VB.net that runs as a scheduled tasks. It internally calls another executable and it has to access a mapped drive. Apparently windows has issues with scheduled tasks accessing mapped drives when the user is not logged on, even when the authentication credentials are supplied to the task itself. Ok, fine.

要解决这个问题我只是通过我的应用程序的UNC路径。

To get around this I just passed my application the UNC path.

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路径是工作目录。

This is the same implementation I used with the mapped drive, however using the UNC path, the process is not behaving as if the UNC path is the working directory.

是否有任何已知问题设置ProcessStartInfo.WorkingDirectory到一个UNC路径?如果不是,我是什么做错了吗?

Are there any known issues setting ProcessStartInfo.WorkingDirectory to an UNC path? If not, what am I doing wrong?

推荐答案

您的问题,映射驱动器时,用户没有登录的是,它们不存在。驱动器只能映射并提供给当前登录的用户。如果没有人登录,没有驱动器映射。

Your problem with mapped drives when users aren't logged in is that they don't exist. Drives are only mapped and available to the currently logged in user. If no one is logged in, no drives are mapped.

作为一种变通方法,您可以通过CMD运行,调用PUSH​​D将您的UNC映射到幕后的驱动器,然后执行你的code。我从我的SYSTEM32复制的tree.com文件,并把它放在我的文件服务器上的tree4.com,这code按预期工作(我也将标准输出重定向,所以我能看到的结果调用,但是这不是必要的)

As a workaround you can run through CMD, call PUSHD which will map your UNC to a drive behind the scenes and then execute your code. I've copied the tree.com file from my system32 and placed it on my file server as "tree4.com" and this code works as expected (I'm also redirecting standard output so I can see the results of the call but that's not necessary)

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