1

This is a simple form with four buttons that will launch four programs. All four programs are related to each other and reside in the same directory.

I've been struggling on porting this program from C++ to C# and one of the major issues was getting WorkingDirectory to function. I have finally sorted that nuisance out. However, what I need now is to prompt the user (on program load) to select the directory of the four aforementioned executables my program should look for.

Here's some code for one of the buttons (there are three others exactly like it with only minor changes obviously):

private void button1_Click(object sender, EventArgs e)
    {
        string myDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        string gameDir = Path.Combine(myDir, "PATH TO BE SPECIFIED BY USER");
        string gameExe = Path.Combine(gameDir, "Proc1.EXE");
        Process proc1 = new Process();
        proc1.StartInfo.FileName = gameExe;
        proc1.StartInfo.WorkingDirectory = gameDir;
        proc1.SynchronizingObject = this;
        proc1.EnableRaisingEvents = true;
        proc1.Start();
    }

Line 4 "PATH TO BE SPECIFIED BY USER" is where the user's directory would be located.

Now, if I were to hard code the directory that's on my machine, it works flawlessly. The problem will arise when users have these 4 executables installed to a different directory than I.

What I'm afraid is going to happen is my program would need to rely on external configuration files for this to work. I admit, I'm very, very new to programming and wouldn't even know where to begin with that.

I hope I've explained my problem enough. if not, please let me know. Thanks for your help!

Steve
  • 11
  • 5
  • 1
    Are you looking for a folder browse dialog? http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.aspx – shf301 May 08 '14 at 18:19
  • I'm assuming that's what I need as the user will be prompted only once, when the program first loads, to select the directory that contains the executables my program should look for. – Steve May 08 '14 at 18:22

3 Answers3

1

Do not use Path.Combine, instead ask the user to select the folder using FolderBrowserDialog and then set the gameDir path as

FolderBroserDialog fbd = new FolderBrowserDialog();

DialogResult result = fbd.ShowDialog();
if( result != DialogResult.OK )
{
  // exit the program as user has not specified any directory
  return;
}

string gameDir = folderBrowserDialog1.SelectedPath;
Manish Dalal
  • 1,768
  • 1
  • 10
  • 14
  • I don't completely understand where that code is supposed to go and what I'm supposed to remove. [Here is my code](http://pastebin.com/QtHfvene). I'm very new to coding, my apologies for not understanding. – Steve May 10 '14 at 04:58
  • Replace the line `string gameDir = Path.Combine(myDir, "PATH TO BE SPECIFIED BY USER");` with [all] the code above. – Manish Dalal May 11 '14 at 18:53
  • I would also like to suggest you that in order to improve the game player's experience, you should save the gameDir and other settings somewhere (configuration file or registry, etc) and provide user with an option to update/change this. May be you are already doing it, but I just thought that it would be better to suggest :P :) – Manish Dalal May 11 '14 at 18:55
  • Hi, Manish. I really appreciate your help with this. I have [updated my code](http://pastebin.com/xGpBsy7A) last night after following some guides online and I would like for you to examine it to see what I've got so far. I've added a button that allows the user to select their directory. However, I don't know how to save their selection and have the other four "launch" buttons read the directory they chose. [Here's the form](http://i.imgur.com/S9hd9QT.png). Notice the new button at the top. When they choose their directory, the other four buttons should be able to find the executables. – Steve May 11 '14 at 20:00
  • @Steve: in your form class, create a string variable to store the game Directory (inside class, but outside the event handlers). When user clicks the 'Choose CM installation directory) use the FolderBrowserDialog to let the user select, and store the fbd.SelectedPath in the variable created above. Use that string to read game directory on click of other buttons – Manish Dalal May 11 '14 at 20:06
  • In the pastebin link I gave you, lines 24, 39, 54, and 69 are the four "executable" buttons. Line 89 is the "choose directory" button. According to your code above, each button would require choosing the directory. I would like that new button to be the one where the user selects their directory and have the other four buttons work using their selection. Obviously I need to save their choice in a configuration file as well. – Steve May 11 '14 at 20:08
  • @Steve: Ok, I missed your pastebin link. Please check http://pastebin.com/KQ2hzWAf link. My updates are in lines 24, 31-35, 53-57, 74-78, 95-99, 119, 122. HTH – Manish Dalal May 11 '14 at 20:30
  • Manish, that works! The four buttons now require the directory to be chosen. Does the program save the selection to a file somewhere on the system? What I mean is, if the user closes the application and relaunches it, will their directory be remembered? It doesn't seem to work like that for me. – Steve May 11 '14 at 21:28
  • No, as there is no code written to save and load these settings to/from anywhere, the selected directory will not be saved. You can write code for the same after line 122 (in pastebin code) within the conditional statement of line 121 (if fbd.ShowDialog() == DialogResult.OK) { gameDir = fbd.SelectedPath; // code to save } ......... in Form_Load event handler write code to load the saved settings (if exists) – Manish Dalal May 11 '14 at 21:36
  • Is [this](http://pastebin.com/20AKc6Az) what you mean? Those are from lines 116-127. As for the Form1_load, what is the loading code? All the examples I find seem to give me errors I'm unable to fix. – Steve May 11 '14 at 22:28
0

Will your application be in the same directory as the 4 programs it runs? If so you can just use

System.Environment.CurrentDirectory

which returns the location of the current exe.

Chris Webb
  • 11
  • 1
  • I never thought about that. I mean, I **could** inform the user that my program must be placed in the directory of these executables. I'm trying to get it to where that wouldn't be necessary, so the user could place my program anywhere they'd like. Then again, they could always create a shortcut to my program if it does indeed need to be placed in that directory. Maybe later I'll make it a hassle-free process. – Steve May 08 '14 at 18:35
0

I belive what you are looking for can be found here: Best practice to save application settings in a Windows Forms Application or http://msdn.microsoft.com/en-us/library/a65txexh.aspx

And (Folder browsing): http://msdn.microsoft.com/en-us/library/aa984305(v=vs.71).aspx

This helped me.

Community
  • 1
  • 1
Stenart
  • 3
  • 4