Register file extension in window registry?

2020-01-28 09:35发布

I want to register my own project extension in window registry. I searched on google, at least i found this code, this works well, but I don't understand one line. What is meaning of "%L".

The C# code is

string ext = ".ext";
        RegistryKey key = Registry.ClassesRoot.CreateSubKey(ext);
        MessageBox.Show(exePath);
        key.SetValue("", "My Project");
        key.Close();

        key = Registry.ClassesRoot.CreateSubKey(ext + "\\Shell\\Open\\command");
        //key = key.CreateSubKey("command");

        key.SetValue("", "\"" + Application.ExecutablePath + "\" \"%L\"");
        key.Close();

        key = Registry.ClassesRoot.CreateSubKey(ext + "\\DefaultIcon");
        key.SetValue("", Application.StartupPath + "\\icon.ico");
        key.Close();

that is line which confuse me,

 key.SetValue("", "\"" + Application.ExecutablePath + "\" \"%L\"");

Please explain, I'm very thankful to you in advance.

3条回答
家丑人穷心不美
2楼-- · 2020-01-28 09:51

%L is the "long name" of the file who's association has invoked your program. On modern operating systems it's identical to %1 (short name).

查看更多
走好不送
3楼-- · 2020-01-28 09:52

If your application executable is in C:\your dir\your program.exe the line is translated to:

"C:\your dir\your program.exe" "%L"

%L is translated to the file you're opening, so your program is executing having that file as the first parameter

查看更多
干净又极端
4楼-- · 2020-01-28 09:56

In order to understand the %L you need to understand which program is going to be doing the reading from the registry.

In this case, the verbs specified under `HKCR.ext\shell*' are read and processed by explorer.exe when it is launching programs associated with extensions.

There does not seem to be a definitive list of what explorer looks for when creating a command line. However, %L tells explorer that the program its launching will accept the long form of the filename on the command line. and long file names can have spaces in them.

Which is why programs that take long file names on the command line need to be able to handle spaces - explorer does this itself by using ',' as a command line seperator, or by allowing filenames on the command line to be enclosed in quotes.

查看更多
登录 后发表回答