输入文件位置时,将空格作为可执行文件的参数。

huangapple go评论49阅读模式
英文:

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 (`&quot;`) for the string parameter (and escaping them):
 

pathToHexFile = "&quot;C:\Users\Target Files\singlefile.txt&quot;"

Or in your case when `pathToHexFile ` is a parameter for the method:

pathToHexFile = "&quot;" + pathToHexFile + "&quot;";


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 (`&#39;`) will indeed split the string into two parameters. 

Tested on .NET Framework 4.7.2

</details>



huangapple
  • 本文由 发表于 2023年7月20日 14:32:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定