views:

386

answers:

1

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.

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()

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.

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

A: 

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.

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
Chris Haas