Search code examples
c#windowsbatch-filecmdprocess.start

The system cannot find the path specified from ProcessStartInfo


I've been working on this all morning so I'm here for some help. Read many SO questions on this and tried all the solutions without resolving my issue.

I started by just using Process.Start(batchfile.bat); It works on some computers but not others. On the computers where the batch file doesn't work, the command prompt opens and says

'ogr2ogr.exe' is not recognized as an internal or external command, operable program or batch file.

The command works fine if you open a command prompt and run it but not by double clicking the batch file. So I started trying to use ProcessStartInfo.

I am selecting a file and reading the file. What I am reading from the file is not important.

strFilePath = Path.GetDirectoryName(openFileDialog1.FileName);

At this point strFilePath is = O:\\03 Supervisors\\04_Production\\test O: is a mapped drive.

I create a batch file in that location and write some commands to it.

File.CreateText(strFilePath + "\\" + "kml2shp.bat").Dispose();

I write to my batch file the command to be executed.

ogr2ogr.exe -f "ESRI Shapefile" "O:\03 Supervisors\04_Production\test\Final.shp"  "O:\03 Supervisors\04_Production\test\_Map.kml"  
PAUSE

My code to start the process is now

try
{
    ProcessStartInfo startInfo = new ProcessStartInfo();

    if (File.Exists(strFilePath + "\\kml2shp.bat") == false)
    {
        MessageBox.Show("File is missing");
    }
    startInfo.FileName = strFilePath + "\\kml2shp.bat";
    startInfo.LoadUserProfile = false;
    startInfo.Domain = "mydomain";
    startInfo.UserName = "myusername";
    startInfo.Password = MakeSecureString("mypassword");
    startInfo.UseShellExecute = false;

    Process.Start(startInfo);

}
catch (Win32Exception w32E)
{
    MessageBox.Show(w32E.ToString());
}

private static SecureString MakeSecureString(string text)
{
    SecureString secure = new SecureString();
    foreach (char c in text)
    {
        secure.AppendChar(c);
    }

    return secure;
}

This results in enter image description here

If I don't define the domain, user, and password the command prompt says.

'ogr2ogr.exe' is not recognized as an internal or external command, operable program or batch file.

When I do, it can't find the file.

So why does my File.Exists(strFilePath + "\\kml2shp.bat") pass, but the process can't find the file?

I have tried

startInfo.WorkingDirectory = strFilePath + "\\";
startInfo.FileName = "kml2shp.bat";

This results in enter image description here

Seems to look like it doesn't like the path in the process being O:\\03 Supervisors\\04_Production\\test

I assume the space in the path but I have tried 20 different ways to try to enclose it in double quotes with no success. Any ideas?


Solution

  • Mapped drives aren't global, they are specific to the logon session. By providing credentials, you're asking Process.Start to run the program in a separate logon session, so when the process attempts to start, your mapped drive is no longer available. Hence the "cannot find the path" and "the directory name is invalid" errors.

    It doesn't sound as if you need the credentials anyway, so just get rid of them and fix the original problem: the batch processor can't find ogr2ogr.exe. The easiest solution is probably to provide the full path to the executable as part of the batch file. Or if the batch file isn't actually necessary (it isn't clear from the question why you're using one in the first place) you could try running the executable directly using Process.Start.