英文:
Inputting file location with spaces as arguments for an executable
问题
I need to input the file location from OpenFileDialog, as an argument to a .exe file, the problem is the file name has spaces and when they are called by the exe file as arguments, it breaks up on the spaces. For example if I where to get "C:\Users\Target Files\singlefile.txt", the string would be split between "Target" and "File"
private static void CreateBinFromHex(String pathToHexFile)
{
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = System.IO.Directory.GetCurrentDirectory() + "\\hex2bin.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = pathToHexFile;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch(SystemException e)
{
MessageBox.Show(e.Message);
}
}
I tried different combinations in the terminal and found that enclosing the argument in a single quotes allows for a string including spaces to be used, but I cannot find a way to apply that in my program. For example---> the.exe 'C:\Users\Target Files\singlefile.txt' works perfectly.
英文:
I need to input the file location from OpenFileDialog, as an argument to a .exe file, the problem is the file name has spaces and when they are called by the exe file as arguments, it breaks up on the spaces. For example if I where to get "C:\Users\Target Files\singlefile.txt", the string would be split between "Target" and "File"
private static void CreateBinFromHex(String pathToHexFile)
{
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = System.IO.Directory.GetCurrentDirectory() + "\\hex2bin.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = pathToHexFile;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch(SystemException e)
{
MessageBox.Show(e.Message);
}
}
I tried different combinations in the terminal and found that enclosing the argument in a single quotes allows for a string including spaces to be used, but I cannot find a way to apply that in my program. For example---> the.exe 'C:\Users\Target Files\singlefile.txt' works perfectly.
答案1
得分: 1
pathToHexFile = ""C:\Users\Target Files\singlefile.txt""
或者在您的情况下,当`pathToHexFile`是方法的参数时:
pathToHexFile = """ + pathToHexFile + """;
这样,进程启动将以如下方式调用您的exe:
the.exe "C:\Users\Target Files\singlefile.txt"
双引号将把字符串作为整体处理。
作为对比,单引号(`'`)将确实将字符串分为两个参数。
已在.NET Framework 4.7.2上测试。
<details>
<summary>英文:</summary>
You can achieve the behavior you want with adding an extra double quotations (`"`) for the string parameter (and escaping them):
pathToHexFile = ""C:\Users\Target Files\singlefile.txt""
Or in your case when `pathToHexFile ` is a parameter for the method:
pathToHexFile = """ + pathToHexFile + """;
This way the the process start will call your exe like this:
the.exe "C:\Users\Target Files\singlefile.txt"
And the double quotations will threat the string as a whole.
As a comparison the single quotation (`'`) will indeed split the string into two parameters.
Tested on .NET Framework 4.7.2
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论