简体   繁体   中英

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

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

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

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

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

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. Take a look!

The Libraries in Windows 7 are not actual folders, but rather collections of physical folders. Because of that there is no way to convert to a standard c:\\foldername structure.

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

Environment.GetFolderPath(Environment.SpecialFolder)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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