简体   繁体   English

如何从命令行参数返回完整路径

[英]How to return the full path from a command line argument

I have an application that can be run from Explorer, and passes the selected directory to the application. 我有一个可以从资源管理器运行的应用程序,并将所选目录传递给该应用程序。 So I can use the following code: 所以我可以使用以下代码:

private void frmMain_Shown(object sender, EventArgs e)
    {
        //open the dir
        DirectoryInfo d = new DirectoryInfo(cmdArgs);
        SelectDirectoryInTree(d);
    }

This however fails if the user selects a Special Folder. 但是,如果用户选择特殊文件夹,则失败。 The path returned for these folders is different. 这些文件夹返回的路径不同。 So, for example, if the user selects Libraries\\Documents folder, (or any other folder in there) the returned DirectoryInfo is ::{xxxxx-xxxx-xxxxx-xxx-xxxxx}\\Documents.library-ms 因此,例如,如果用户选择Libraries\\Documents文件夹(或其中的任何其他文件夹),则返回的DirectoryInfo为::{xxxxx-xxxx-xxxxx-xxx-xxxxx}\\Documents.library-ms

The specific exception: 具体的例外:

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()

Whats the simplest way to get the correct folder supplied by the explorer shell? 获取资源管理器外壳程序提供的正确文件夹的最简单方法是什么?

Have you considered using the Windows API Code Pack ? 您是否考虑过使用Windows API代码包 It contains a number of functions for the Windows Shell. 它包含Windows Shell的许多功能。

The User Libraries Folder is a special folder, and as such, it has a special parsing name, 用户库文件夹是一个特殊的文件夹,因此,它具有特殊的解析名称,

::{031E4825-7B94-4DC3-B131-E946B44C8DD5}

If you were to copy and paste the above into your explorer or into the run command you would get the user libraries folder. 如果要将以上内容复制并粘贴到资源管理器或run命令中,将获得用户库文件夹。

Enumerating over those folders with the Shell interfaces, you would be able to retrieve all folders and files inside each of the libraries, and each of them would resolve to an on-disk location. 使用Shell界面枚举这些文件夹,您将能够检索每个库中的所有文件夹和文件,并且每个库都可以解析到磁盘上的位置。 In reality, each folder and file in each library will each resolve to a parsing location which is usually the physical (or in some cases, network) location of the file and folder in question. 实际上,每个库中的每个文件夹和文件都将解析到一个解析位置,该解析位置通常是所讨论的文件和文件夹的物理(或在某些情况下为网络)位置。

As far as enumerating all the files in a library is concerned, however, you need to use the shell API, because of the special kind of folders they are (libraries combine several physical locations into one "virtual" location.) 但是,就枚举库中的所有文件而言,您需要使用shell API,因为它们是一种特殊的文件夹(库将多个物理位置合并为一个“虚拟”位置)。

You should learn about parsing names. 您应该了解有关解析名称的知识。 There's a lot of interesting things you can do with the system once you have something's parsing name, including bringing up property sheets with ShellExecuteEx (which is a function in the Win32 Api). 一旦有了名称的解析名称,就可以对系统进行很多有趣的操作,包括使用ShellExecuteEx(这是Win32 Api中的函数)调出属性表。

There is a sample program in the Windows API Code Pack called "KnownFolders Browser" it should elucidate many things regarding the shell for you. Windows API代码包中有一个名为“ KnownFolders Browser”的示例程序,该程序应为您阐明有关外壳的许多内容。

This is for future people since this is so old. 这是给未来的人的,因为它已经很老了。 I followed Nathan's suggestion and posted a full solution with code using Windows API Code Pack at this other StackOverflow question that was wanting something similar. 我遵循内森(Nathan)的建议,并在另一个想要类似的StackOverflow问题上发布了使用Windows API代码包的完整代码解决方案 Take a look! 看一看!

The Libraries in Windows 7 are not actual folders, but rather collections of physical folders. Windows 7中的库不是实际的文件夹,而是物理文件夹的集合。 Because of that there is no way to convert to a standard c:\\foldername structure. 因此,无法转换为标准c:\\ foldername结构。

You could check if that folder is a special folder and resolve it accordingly: 您可以检查该文件夹是否为特殊文件夹,并相应地对其进行解析:

System.Environment.SpecialFolder

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder%28v=vs.110%29.aspx http://msdn.microsoft.com/zh-cn/library/system.environment.specialfolder%28v=vs.110%29.aspx

Environment.GetFolderPath(Environment.SpecialFolder)

http://msdn.microsoft.com/en-us/library/14tx8hby%28v=vs.110%29.aspx http://msdn.microsoft.com/zh-CN/library/14tx8hby%28v=vs.110%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM