今天在数据备份和恢复时,遇到了路径中空格问题,只好求助API,搜,GetShortPathName(),查MSDN,速度太慢了,算了,PInvoke一个专注API的知识库,不过意外的发现,PInvoke.net 做了一个基于VS的插件,想的周到,试用一下,很爽,不敢独享,推荐给大家......
安装PInvokeVsAddin, 重启VS,菜单上多了一项PInvoke.net,OK
打开项目,Insert PInvoke signatures...
开搜...
如果对API的使用不熟悉,PInvoke 提供了网站的例子帮助,"Go to funcation in PInvoke.net",链接已经在插件上提供了,例如:GetShortPathName插件自动导航到 http://pinvoke.net/default.aspx/kernel32.GetShortPathName ,VB,C#,Other 的实例.
一个简单的例子:
Code
//http://cnblogs.com/winzheng
class Program
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string lpszLongPath,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder lpszShortPath,
uint cchBuffer);
static void Main()
{
Console.WriteLine(GetShortPathName(Environment.CurrentDirectory));
Console.ReadLine();
}
/// <summary>
///
/// </summary>
/// <param name="longName">The long name path</param>
/// <returns>A short name path string</returns>
public static string GetShortPathName(string longName)
{
StringBuilder shortNameBuffer = new StringBuilder(256);
uint bufferSize = (uint) shortNameBuffer.Capacity;
uint result = GetShortPathName(longName, shortNameBuffer, bufferSize);
if (result != 0)
return shortNameBuffer.ToString();
return longName;
}
//http://cnblogs.com/winzheng
class Program
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string lpszLongPath,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder lpszShortPath,
uint cchBuffer);
static void Main()
{
Console.WriteLine(GetShortPathName(Environment.CurrentDirectory));
Console.ReadLine();
}
/// <summary>
///
/// </summary>
/// <param name="longName">The long name path</param>
/// <returns>A short name path string</returns>
public static string GetShortPathName(string longName)
{
StringBuilder shortNameBuffer = new StringBuilder(256);
uint bufferSize = (uint) shortNameBuffer.Capacity;
uint result = GetShortPathName(longName, shortNameBuffer, bufferSize);
if (result != 0)
return shortNameBuffer.ToString();
return longName;
}
一个将路径改为8.3格式的功能在动一动鼠标的瞬间完成,是不是值得推进,不过,工具只不是加快我们的开发速度,尽量不去把时间花到写重复代码上,对于不熟悉的技术还需要自己仔细琢磨,以免离开工具,我们什么都不会写了....
强烈推荐...例子源码