Click here to Skip to main content
15,030,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a C# application which creates files in custom format. I have the exe of this application linked to the file type of these custom files. So when some one double clicks the file the application opens the specified file.

This is working well, except the file name passed via command line argument to this application is shortened. I am using Environment.GetCommandLineArgs() function to get the argument when the file is double clicked. The file name received via this function is shortened and is in the 8 dot 3 format. I would like to get the long file name. What should I do to get the long file name in my application?

Any suggestion or help is appreciated very much!

Thanks
Kalyani
Posted
Comments
[no name] 24-Aug-12 11:21am
   
You might try reading through http://www.milkpail.com/buki/longname.htm and see if that helps.
[no name] 24-Aug-12 11:29am
   
Or http://support.microsoft.com/kb/142275 might help also
Sergey Alexandrovich Kryukov 24-Aug-12 11:30am
   
It cannot be. Are you sure the problem is not in blank space inside name (in this case, you have to put the whole parameter in "" quotation marks). If this is really 8 dot 3 ("DOS") file names, which should not event exist, you screw up something very well. To start with, could you make a short but complete code sample to reproduce this problem only?
If so, put it in your question using "Improve question" above.
--SA
Maciej Los 24-Aug-12 12:41pm
   
Agree ;)
Sergey Alexandrovich Kryukov 24-Aug-12 14:09pm
   
And OP accepted a really bad answer nevertheless -- please see.
--SA
Thomas Daniels 24-Aug-12 12:53pm
   
How do you register your file types?
That's important.
Sergey Alexandrovich Kryukov 24-Aug-12 14:08pm
   
Please see: OP accepted a very bad answer... this is not a solution. The real solution would be: don't screw up long names.
--SA
Maciej Los 24-Aug-12 14:24pm
   
Perfect answer, my virtual 5!
Sergey Alexandrovich Kryukov 24-Aug-12 17:12pm
   
Thank you, Maciej; I almost sorry I did not put this as an answer... :-)
--SA

1 solution

C#
using System;
using System.Runtime.InteropServices;
using System.Text;

public class _Main
{
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern int GetLongPathName(
        [MarshalAs(UnmanagedType.LPTStr)]
        string path,
        [MarshalAs(UnmanagedType.LPTStr)]
        StringBuilder longPath,
        int longPathLength
        );
        
    public static void Main()
    {
        StringBuilder longPath = new StringBuilder(255);
        GetLongPathName(@"D:\MYTEMP~1\RESOUR~1\sql.txt", longPath, longPath.Capacity);
        Console.WriteLine(longPath.ToString());
    }
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900