Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to make code that for given directory location in variable for example:
string _path = "c:/windows"

on button click event to start file explorer at that path?

im asking something exactly how start command is working in cmd prompt for example in cmd is: start "start c:/windows", any ideas?

hope you understand what i mean
Posted
Updated 13-Aug-23 0:09am

C#
// required in addition to other 'using necessary
using System.Diagnostics;
using System.IO;

private void OpenFolder(string folderPath)
{
    if (Directory.Exists(folderPath))
    {
        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            Arguments = folderPath,
            FileName = "explorer.exe";
        };

        Process.Start(startInfo);
    }
    else
    {
        MessageBox.Show(string.Format("{0} Directory does not exist!", folderPath));
    }
}
Note: if I was writing this in production code, I'd probably wrap the whole thing in a Try/Catch to intercept possible access permission errors.
 
Share this answer
 
v2
you can try by executing run command as below
C#
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
string _path = "c:/windows";
startInfo.Arguments = string.Format("/C start {0}", _path);
process.StartInfo = startInfo;
process.Start();
 
Share this answer
 
The easy way is to ask Google. It take far less time to find the answer that way than to write the question itself here : https://www.google.ca/?gws_rd=ssl#q=open+file+in+explorer+c%23[^]

The question has been answered here : http://stackoverflow.com/questions/1073353/c-how-to-open-windows-explorer-windows-with-a-number-of-files-selected[^]

I think that you can open either a folder or select a file that way... but I'm not sure. For sure, I use it to open containing folder with initial selection in my application and it works quite well. I don't remember what I use but it should be very similar to what I have found using a search.

Update
To open a folder, you just specify folder name without /select, part. Something like explorer c:\folder_name.

/select option requires an existing file or folder and open its parent and select the item. Thus both options are available.
 
Share this answer
 
v3
Comments
BillWoodruff 11-Dec-14 22:18pm    
#1 Vote

The OP is asking how to open an Explorer window of a specific Directory, not how to open a file.

If you're "not sure," and "don't remember" what works for you, is this a "solution" ?
Philippe Mori 11-Dec-14 22:47pm    
I have updated my solution. Effectively, select option would open the parent folder if you don't specify a file so better to just past the folder name as an argument.

Well even though if was not the solution, given that OP would only have to remove the /select, part it was not too far of a solution. The main idea was that it was faster to do a Google search for such question.
SrgjanX 12-Dec-14 14:03pm    
thanks its done :)
it is worth noting that explorer requires that the path use back slashes and not forward slashes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900