Hello

My application creates and reads files from the same folder. So I am using appliaction.startuppath property to get the current path so far. Now I have included a open file dialog box to browse and load another file from other folder. But after this dialog loads the file from another folder, it seems the application startup path has changed.
I want to bring the application path to the same folder as application is starting from.How to do this? or what is the normal procedure usually followed in case of path tracking while the application is running.

Thank you

Recommended Answers

All 6 Replies

Thank you. That partly solved my issue.
When I set that property the application startup path is not changing, so no problem.
Here is my code

private void btnFileOpen_Click(object sender, EventArgs e)
        {
            string newTestPath = Path.Combine(Application.StartupPath, "data\\");
            openFileDialogPreviousData.InitialDirectory = newTestPath;// "C:";//@newTestPath;
            openFileDialogPreviousData.RestoreDirectory = true;
            openFileDialogPreviousData.ShowDialog();
            txbxFilePath.Text = openFileDialogPreviousData.FileName;
         }

But I have one more issue. Even though on every subsequent click even I am loading the initial directory property, only on the first click it loads my data. On all subsequent clicks the dialog starts from the previous file and folder selection. But application path has not changed - so no other issues.
I need this dialog to start from the same initial folder, whatever the previous selection was!
Thank you
How this can be solved?

Found the solution.

See here

With "normal" paths (local/UNC) the InitialDirectory property works as long as user doesnot change current folder in the dialog. When user browses to another directory, the InitialDirectory property is ignored, even when set after that user action. Workaround is to create a new instance of FileDialog.
Tested with OpenFileDialog, Win XP

So I created a separate instance on each click.

Here is my code now

private void btnFileOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialogPreviousData = new OpenFileDialog();
            string newTestPath = Path.Combine(Application.StartupPath, "data\\");
            openFileDialogPreviousData.InitialDirectory = newTestPath;// "C:";//@newTestPath;
            openFileDialogPreviousData.Filter = "RCP Data Files|*.txt";
            openFileDialogPreviousData.Title = "Previous Record to Open";
            openFileDialogPreviousData.RestoreDirectory = true;
            openFileDialogPreviousData.ShowDialog();
            txbxFilePath.Text = openFileDialogPreviousData.FileName;
            openFileDialogPreviousData.Dispose();
         }

btw I am not sure this dispose is needed.

Thank you

Yes the dispose is needed. You also need to check the dialog result. With the code you posted it will still set the filename even if the user clicks "cancel" or "X" in the dialog.

private void button8_Click(object sender, EventArgs e)
    {
      using (OpenFileDialog openFileDialogPreviousData = new OpenFileDialog())
      {
        string newTestPath = System.IO.Path.Combine(Application.StartupPath, "data\\");
        openFileDialogPreviousData.InitialDirectory = newTestPath;// "C:";//@newTestPath;
        openFileDialogPreviousData.Filter = "RCP Data Files|*.txt";
        openFileDialogPreviousData.Title = "Previous Record to Open";
        openFileDialogPreviousData.RestoreDirectory = true;
        if (openFileDialogPreviousData.ShowDialog() == DialogResult.OK) //Make sure they hit OK
        {
          txbxFilePath.Text = openFileDialogPreviousData.FileName;
        }
      } //End of using calls .Dispose() for you
    }
commented: helpful +1

Thank you Sknake!

I got it and modified.

I'm glad you got it working

Please mark this thread as solved if you have found an answer to your question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.