如何从一个命令行参数返回完整路径 [英] How to return the full path from a command line argument

查看:193
本文介绍了如何从一个命令行参数返回完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从浏览器中运行,并将选定的目录到应用程序的应用程序。因此,我可以使用下面的code:

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);
    }

如果用户选择了一个特殊文件夹然而,这将失败。返回这些文件夹的路径是不同的。因此,举例来说,如果用户选择库\文件文件夹(或有任何其他文件夹中)返回的DirectoryInfo的是 :: {xxxxx- XXXX-XXXXX-XXX-XXXXX} \ Documents.library毫秒

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

在特定的异常:

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?

推荐答案

你有没有考虑使用的 Windows API的code包?它包含了许多用于Windows外壳的功能。

Have you considered using the Windows API Code Pack? It contains a number of functions for the 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.

列举了与壳牌接口这些文件夹,你就可以取回里面每个库的所有文件夹和文件,他们每个人将解析到一个磁盘上的位置。在现实中,每个文件夹和文件中的每个库将每个解析到解析位置通常是有问题的文件和文件夹的物理(或在某些情况下,网络)位置。

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. 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.

至于列举库中的所有文件而言,但是,你需要是因为特殊的文件夹,它们是使用shell API,(库组合几个物理位置成一个虚拟的位置。)

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

您应该了解解析名称。有很多有趣的事情,你可以使用系统;一旦你有什么事情的解析名,其中包括抚养属性页用的ShellExecuteEx(这是在WIN32 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).

有一个在Windows API的code包的示例程序叫做KnownFolders浏览器,它应阐明有关外壳为你很多东西。

There is a sample program in the Windows API Code Pack called "KnownFolders Browser" it should elucidate many things regarding the shell for you.

这篇关于如何从一个命令行参数返回完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆