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.
Alina ValkerPosted Jul 21, 2019, 11:17 AM
Try long path tool. I have been using it for a while and can delete, copy, move and bulk rename long path files or locked file.
George HendricksonPosted Mar 10, 2015, 8:56 PM
Has anyone had any problems with getting the short file name if the file name is stored on a hard drive set as drive E:? Short file names aren't coming back for me if i try getting them from drive E:? It is an NTFS partition. Not sure if its a bug in interop or if it has something to do with the format of my drive...
Giancarlo ManciaPosted Oct 14, 2010, 4:45 AM
ok