[英]How to return the full path from a command line argument
我有一個可以從資源管理器運行的應用程序,並將所選目錄傳遞給該應用程序。 所以我可以使用以下代碼:
private void frmMain_Shown(object sender, EventArgs e)
{
//open the dir
DirectoryInfo d = new DirectoryInfo(cmdArgs);
SelectDirectoryInTree(d);
}
但是,如果用戶選擇特殊文件夾,則失敗。 這些文件夾返回的路徑不同。 因此,例如,如果用戶選擇Libraries\\Documents
文件夾(或其中的任何其他文件夾),則返回的DirectoryInfo為::{xxxxx-xxxx-xxxxx-xxx-xxxxx}\\Documents.library-ms
具體的例外:
System.ArgumentException: The path is not of a legal form.
at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength)
at System.IO.Path.GetFullPathInternal(String path)
at System.IO.DirectoryInfo.Init(String path, Boolean checkHost)
at System.IO.DirectoryInfo..ctor(String path)
at FindIt.frmMain.frmMain_Shown(Object sender, EventArgs e) in d:\C#\+Other\FindIt\frmMain.cs:line 476
at System.Windows.Forms.Form.OnShown(EventArgs e)
at System.Windows.Forms.Form.CallShownEvent()
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
獲取資源管理器外殼程序提供的正確文件夾的最簡單方法是什么?
您是否考慮過使用Windows API代碼包 ? 它包含Windows Shell的許多功能。
用戶庫文件夾是一個特殊的文件夾,因此,它具有特殊的解析名稱,
::{031E4825-7B94-4DC3-B131-E946B44C8DD5}
如果要將以上內容復制並粘貼到資源管理器或run命令中,將獲得用戶庫文件夾。
使用Shell界面枚舉這些文件夾,您將能夠檢索每個庫中的所有文件夾和文件,並且每個庫都可以解析到磁盤上的位置。 實際上,每個庫中的每個文件夾和文件都將解析到一個解析位置,該解析位置通常是所討論的文件和文件夾的物理(或在某些情況下為網絡)位置。
但是,就枚舉庫中的所有文件而言,您需要使用shell API,因為它們是一種特殊的文件夾(庫將多個物理位置合並為一個“虛擬”位置)。
您應該了解有關解析名稱的知識。 一旦有了名稱的解析名稱,就可以對系統進行很多有趣的操作,包括使用ShellExecuteEx(這是Win32 Api中的函數)調出屬性表。
Windows API代碼包中有一個名為“ KnownFolders Browser”的示例程序,該程序應為您闡明有關外殼的許多內容。
這是給未來的人的,因為它已經很老了。 我遵循內森(Nathan)的建議,並在另一個想要類似的StackOverflow問題上發布了使用Windows API代碼包的完整代碼解決方案 。 看一看!
Windows 7中的庫不是實際的文件夾,而是物理文件夾的集合。 因此,無法轉換為標准c:\\ foldername結構。
您可以檢查該文件夾是否為特殊文件夾,並相應地對其進行解析:
System.Environment.SpecialFolder
http://msdn.microsoft.com/zh-cn/library/system.environment.specialfolder%28v=vs.110%29.aspx
Environment.GetFolderPath(Environment.SpecialFolder)
http://msdn.microsoft.com/zh-CN/library/14tx8hby%28v=vs.110%29.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.