简体   繁体   中英

Argument length when folder or file contains spaces

I have registered file association in windows to open specific file types in application, so that when user double clicks the file it opens on my application exe.

I receive the file path from argument and opens it. But when either folder or file name contains spaces (some time more than one space), the argument length is more than 1 and i don't know how to parse them in to a single file path. I don't have control over the argument being passed since windows passes those on double click to my application exe.

Sample file path: C:\\Sample 1\\file 1.rtf

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

Any suggestions would be appreciated, thanks.

您需要将文件名包装在引号中。

applicationname.exe "C:\Sample 1\file 1.rtf"

You should quote the argument before it's passed in:

myapplication.exe "C:\Sample 1\file 1.rtf"

If we take a look at the association for .sln files for Visual Studio under HKEY_CLASSES_ROOT\\VisualStudio.Launcher.sln\\Shell\\Open\\Command , we can see that the association quotes the filename:

"C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\VSLauncher.exe" "%1"

Your association with your program should do the same. Alternatively, you could join the arguments into a single string:

var path = string.Join(string.Empty, args);

This is less flexible, however. Especially if you need to pass multiple arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM