今天在資料備份和恢複時,遇到了路徑中空格問題,只好求助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;
}
一個將路徑改為8.3格式的功能在動一動滑鼠的瞬間完成,是不是值得推進,不過,工具只不是加快我們的開發速度,盡量不去把時間花到寫重複代碼上,對於不熟悉的技術還需要自己仔細琢磨,以免離開工具,我們什麼都不會寫了....
強烈推薦...例子源碼