Question

I wrote a c# program and I associated it with file extension like DOC in a PC without MS-Office installed. Then, I double-clicked any file which name contains blank characters, my program will be launched to open that file. I used below statement:

string[] args = Environment.GetCommandLineArgs();

and then args[1] will contain full path file name of that file. Then, I can open it. But the problem now is that if the file name contains blank characters, args[1] contains file name different from the real one. As title, if my file is in e:\tmp3 and file name is test uesr=doc.doc, I expected args[1] contains

"e:\tmp3\test user-doc.doc",

but it actually contains

"E:\tmp3\TESTUS~1.DOC"

Could anyone tell me why and how to resolve it? Thanks.

Was it helpful?

Solution

As already mentioned these are 8.3 file names. If you need to convert from a short name to a full name then you can easily do this with C#.

new FileInfo("E:\tmp3\TESTUS~1.DOC").FullName

Going the other way requires a PInvoke call to GetShortPathName. Be aware that this doesn't work on all NTFS volumes as short names can be turned off but they are turned on by default for the volume the OS is on.

class Program
{
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern int GetShortPathName(String pathName, StringBuilder shortName, int cbShortName);

    static void Main(string[] args)
    {
        var fullname = args[0];
        var shortPathBuilder = new StringBuilder(fullname.Length);
        GetShortPathName(fullname, shortPathBuilder, shortPathBuilder.Length);
        var shortname = shortPathBuilder.ToString();
    }
}

OTHER TIPS

You should put double quote marks around the %1 replacement in the shell\open\command registry key. For example:

"C:\Program Files\MyApp\MyApp.exe" "%1"

rather than

"C:\Program Files\MyApp\MyApp.exe" %1

If you don't include the double quote marks, Windows detects that filenames with spaces (or other parameter separators) are unlikely to work, and substitutes the short file name. This is for compatibility with 16-bit Windows programs (the HKCR\shell key was introduced for Windows 3.1).

They are called 8.3 Filenames. Basically, they are an alias for the file in the File Allocation Table that shortens the path to the file.

8.3 refers to "8 characters.. then a dot.. then 3 characters". The three characters are the file extension obviously..

Also, you'll note that TESTUS~1 is 8 characters in length.

As far as I am aware, there isn't really much you can do to stop Windows from doing this. You could format your disk to be NTFS I think (I don't think NTFS is so aggresive with file "aliasing").

The issue is with space character (the blank one), as it will consider it as args[2] i.e. test user-doc.doc will be treated as two args instead of one due to blank character, so you can use sub string function, with calculating total number of args as well, then first concatenate all args from args[1] to args[n] where n is the size of args, this way you can avoid the problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top