[英]Process.start does not find file when useshellexecute = false
I need to call batch-files from my UWP Application.我需要从我的 UWP 应用程序调用批处理文件。 The way to do seems to be Process.Start(), but it says it does not find the file even tough it is definitly there when I follow the path it outputs.这样做的方法似乎是Process.Start(),但它说它没有找到文件,即使我按照它输出的路径确定它确实存在。 Filepath and Working Directory are both given as full paths as is requiered when using shellexecute = false.文件路径和工作目录都作为使用 shellexecute = false 时所需的完整路径给出。
It works when I set useshellexecute = true.当我设置 useshellexecute = true 时它起作用。 Since the full path works here, the file is clearly there.由于完整路径在这里有效,文件显然就在那里。 With shellexecute = true the working directory only specifies where it should search for the file and the command prompt starts in the system32 directory, but I need the working directory to be where the opened batch is located.使用 shellexecute = true 时,工作目录仅指定应搜索文件的位置,命令提示符在 system32 目录中启动,但我需要工作目录位于打开的批处理所在的位置。
Hence the ShellExecute = false.因此,ShellExecute = false。
I tried: 1. ShellExecute = true.我试过: 1. ShellExecute = true。 It finds the file but the working directory is not set correctly.它找到了文件,但工作目录设置不正确。 2. Hardcoding the absolute path to a batch. 2. 硬编码批处理的绝对路径。 Still not found.还是没找到。 3. Setting StartInfo.FileName instead of giving it via argument. 3. 设置 StartInfo.FileName 而不是通过参数给它。 4. relative paths 5. Process.Start(Filename). 4. 相对路径 5. Process.Start(Filename)。 Can't set Working Directory without StartInfo 6. Look at similar questions, but the answer is always what I already have (Use full path when shellexecute = false) Can't set Working Directory without StartInfo 6.看类似的问题,但答案总是我已经有了(当shellexecute = false时使用完整路径)
string executable = args[2];
string path = Assembly.GetExecutingAssembly().CodeBase;
string directory = Path.GetDirectoryName(path);
var startInfo = new ProcessStartInfo(directory + @"\Diagnose\_data\Updater\" + executable);
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = directory + @"\Diagnose\_data\Updater";
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
Process.Start(startInfo);
It should find the file since there is a full, absolute path given and the file is defintily there, but it gives an file not found error.它应该找到该文件,因为给出了一个完整的绝对路径并且该文件绝对存在,但它给出了一个找不到文件的错误。
Use Assembly.Location instead of Application.CodeBase .使用Assembly.Location而不是Application.CodeBase 。 Application.CodeBase
returns the source location of the assembly as a URL, not a file path. Application.CodeBase
将Application.CodeBase
的源位置作为 URL 返回,而不是文件路径。 Assemblies could be loaded from URLs or byte arrays and CodeBase
reflects that.程序集可以从 URL 或字节数组加载, CodeBase
反映了这一点。 It returns something like :它返回类似:
file:///C:/TEMP/LINQPad6/_kighplqc/neuyub/LINQPadQuery.dll
The Windows shell can handle file URLs and translates them to actual file paths. Windows shell 可以处理文件 URL 并将它们转换为实际文件路径。 The OS itself requires file paths though.操作系统本身需要文件路径。
You should use Path.Combine
instead of concatenating strings too, to avoid issues with extra or missing slashes.您也应该使用Path.Combine
而不是连接字符串,以避免出现额外或丢失斜杠的问题。 You should use something like :你应该使用类似的东西:
string path = Assembly.GetExecutingAssembly().Location;
string directory = Path.GetDirectoryName(path);
var execPath=Path.Combine(directory,"Diagnose\_data\Updater",executable);
var startInfo = new ProcessStartInfo(execPath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.