我整个上午都在处理这个问题,所以我来这里寻求帮助。阅读许多关于此的 SO 问题并尝试了所有解决方案但没有解决我的问题。
我刚开始使用 Process.Start(batchfile.bat);
它在某些计算机上有效,但在其他计算机上无效。在批处理文件不起作用的计算机上,命令提示符打开并显示
'ogr2ogr.exe' is not recognized as an internal or external command, operable program or batch file.
如果您打开命令提示符并运行它但不是通过双击批处理文件,则该命令工作正常。所以我开始尝试使用 ProcessStartInfo。
我正在选择一个文件并读取该文件。我从文件中读取的内容并不重要。
strFilePath = Path.GetDirectoryName(openFileDialog1.FileName);
此时strFilePath = O:\\03 Supervisors\\04_Production\\test
O:是一个映射驱动器。
我在该位置创建一个批处理文件并向其中写入一些命令。
File.CreateText(strFilePath + "\\" + "kml2shp.bat").Dispose();
我将要执行的命令写入我的批处理文件。
ogr2ogr.exe -f "ESRI Shapefile" "O:\03 Supervisors\04_Production\test\Final.shp" "O:\03 Supervisors\04_Production\test\_Map.kml"
PAUSE
我现在开始这个过程的代码是
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;
}
如果我没有定义域、用户和密码,命令提示符会显示。
'ogr2ogr.exe' is not recognized as an internal or external command, operable program or batch file.
当我这样做时,它找不到文件。
那为什么我的File.Exists(strFilePath + "\\kml2shp.bat")
通过了,但是进程找不到文件呢?
我试过了
startInfo.WorkingDirectory = strFilePath + "\\";
startInfo.FileName = "kml2shp.bat";
似乎不喜欢进程中的路径是 O:\\03 Supervisors\\04_Production\\test
我假设路径中有空格,但我尝试了 20 种不同的方法来尝试用双引号将它括起来,但没有成功。有什么想法吗?
最佳答案
映射的驱动器不是全局的,它们特定于登录 session 。通过提供凭据,您要求 Process.Start
在单独的登录 session 中运行该程序,因此当进程尝试启动时,您的映射驱动器不再可用。因此出现“找不到路径”和“目录名称无效”错误。
听起来好像您无论如何都需要凭据,所以只需摆脱它们并解决原始问题:批处理器找不到ogr2ogr.exe
。最简单的解决方案可能是提供可执行文件的完整路径作为批处理文件的一部分。或者,如果批处理文件实际上不是必需的(从问题中不清楚您为什么首先使用批处理文件),您可以尝试使用 Process.Start
直接运行可执行文件。
关于c# - 系统找不到从 ProcessStartInfo 指定的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35138013/