# Opening a Folder in VC++



## Jacko28 (Sep 15, 2011)

I've recently started learning VC++ and it's coming along quite nicley, I've now been given a little task by my boss to add a button to an excisting Incident report program. 

All the button needs to do is open a new explorer window and open the folder selected in the incident report program. There isn't a deadline for it, it's just to assist in my learning.

I'm not asking for anyone to do it for me, i just need a few suggestions on how to do this so i'm looking in the correct direction?

Thanks


----------



## temp02 (Sep 15, 2011)

Use ShellExecute, like this:

```
ShellExecute(NULL, "explore", "C:\", NULL, NULL, SW_SHOWDEFAULT);
```
And replace "C:\" with whatever folder path you want to open.
Good luck.


----------



## Jacko28 (Sep 15, 2011)

Cheers, I'm going to go and have a play with this now


----------



## Jacko28 (Sep 15, 2011)

I've got the button working now, it opens the folder which I've stated.

I'm now looking at reading a string the user inputs, from this string I want to be able identify and open the folder using the button. 

Is this in the range of someone at intermediate level? 

Furthermore, is there a function like equalsIgnoreCase in c++ and would this be viable for doing what I need?


----------



## temp02 (Sep 15, 2011)

From the looks of it, you are making an GUI application, (not a console one) right?
If you really need to read the path from the user input as a string, which I don't recommend since writing a path is very tedious not to mention error prone, you can drop a TextBox on the form and use TextBox1.Text instead of "C:\" on the call to ShellExecute, like so:

```
ShellExecute(NULL, "explore", TextBox1.Text, NULL, NULL, SW_SHOWDEFAULT);
```

As for the "equalsIgnoreCase" (JAVA right?), you don't need to worry about that, Windows paths are case insensitive, so you can write something like "c:\dOcUmEnTS anD seTTingS\" and it will still work.

Also, I would recommend you taking a look at SHBrowseForFolder, for a more uniform an simple way of selecting a folder.

Good luck, sorry for the extra late reply, I was away for some hours.


----------



## shuggans (Sep 18, 2011)

Jack28,
I would look into using a FolderBrowserDialog object in your application. This will open a dialog that lets you select a folder.  Then use

```
FolderBrowserDialog1.selecteditem
```
 to return the output string of the folder into a textbox, set the textbox enabled property to false so that they cannot type in the path, they have to get the path by selecting the folder using the folder browser.  
This will endure your program never throws a directory not found exception and crashing itself.
This is what I have always doen and it works quite nicely.


----------



## ctrain (Sep 18, 2011)

shuggans said:


> Jack28,
> I would look into using a FolderBrowserDialog object in your application. This will open a dialog that lets you select a folder.  Then use
> 
> ```
> ...



This is .NET, not native Win32


----------



## shuggans (Sep 19, 2011)

I do this with vb.net all the time:

```
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FolderBrowserDialog1.ShowDialog()
        TextBox1.Text = FolderBrowserDialog1.SelectedPath
        My.Settings.SelectedPath = FolderBrowserDialog1.SelectedPath
        Process.Start("explorer.exe", My.Settings.SelectedPath)
    End Sub
End Class
```

This program in itself is a little silly, however if you use the textbox to save an application setting tht returns the path, you could change it later on if needed instead of hard coding it in.


----------



## Jacko28 (Sep 19, 2011)

Thanks for all of the help guys, I've managed to get it working now. I used OpenFileDialog in the end and I've used a StreamReader to open the file. As of right now I'm not reading from a textbox to find the file, the file's I actually need are all in the same directory/folder so I've just put a path to the folder and added a .txt filter(all files are saved a .txt).

I'll keep this thread updated, becuase I'm going to attempt to read from the textbox when I have some more free time.


----------



## shuggans (Sep 20, 2011)

Good deal!


----------

