ARTICLE

Convert Long to Short File Names in C#

Posted by Rajesh Charagandla Articles | How do I March 17, 2006
Here in this article is very simple code for short to long and long to short file names conversion.
Reader Level:

Hello. Here is very simple code for short to long and long to short file names conversion.

Short to long:

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());

          }

}

Long to short:

using System;

using System.Runtime.InteropServices;

using System.Text;

 

public class _Main

{

          [DllImport("kernel32.dll", CharSet = CharSet.Auto)]

          public static extern int GetShortPathName(

                   [MarshalAs(UnmanagedType.LPTStr)]

                   string path,

                   [MarshalAs(UnmanagedType.LPTStr)]

                   StringBuilder shortPath,

                   int shortPathLength

                   );

       

          public static void Main()

          {

                   StringBuilder shortPath = new StringBuilder(255);

                   GetShortPathName(@"D:\My Temp\ResourseProvider\sql.txt", shortPath, shortPath.Capacity);

                   Console.WriteLine(shortPath.ToString());

          }

}

 

Please note that file must exists on the disk. This occur because file system need to know if other files. Two long names produce same names in short form and if so file system will add unique indexes to them.

Login to add your contents and source code to this article
post comment
     

ok

Posted by gioancarlo mancia Oct 14, 2010
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter