Thursday, April 28, 2011

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;

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(string[] args)
{
try
{
StringBuilder shortPath = new StringBuilder(255);
String exepath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
GetShortPathName(exepath, shortPath, shortPath.Capacity);
Console.WriteLine(shortPath.ToString());
if (args.Length>0)
{
foreach (string str in args)
{
string fpth = str.Trim(Path.GetInvalidPathChars());

FileInfo fi = new FileInfo(fpth);
DirectoryInfo di = new DirectoryInfo(fpth);
if (fi.Exists)
{
GetShortPathName(fpth, shortPath, shortPath.Capacity);
Console.WriteLine(shortPath.ToString());
}
else if (di.Exists)
{
GetShortPathName(fpth, shortPath, shortPath.Capacity);
Console.WriteLine(shortPath.ToString());
}
else
{
Console.WriteLine("Given Input path doesnt exits\n");
}
}
}
else
{
Console.WriteLine("Usage:\nShortPath.exe \"c:\\your folder\\Your path\"");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

My shared items