Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenFileDialog path issue

I have a OpenFileDialog which should open up a specific path say %ProgramData% when 'Browse' clicked for the first time the user uses the application. And for all successive terms, it should open up the last used folder.

I tried:

        OpenFileDialog ofd = new OpenFileDialog();
        ofd.InitialDirectory = "C:\ProgramData";
        ofd.RestoreDirectory = true;
        ofd.FileName = "";
        DialogResult dr = ofd.ShowDialog();

The problem here is, it opens up "C:\ProgramData" every time, even if I change path while looking for the required file. Is there a specific property that I should set or do I have to programmatically keep track of the usage of the OpenFileDialog and set path accordingly?

like image 951
dushyantp Avatar asked Jan 28 '26 08:01

dushyantp


1 Answers

Try this:

you are resetting intialdirectory to C:\ProgramData on button click

public partial class Form1 : Form
    {
           OpenFileDialog ofd = new OpenFileDialog();

        public Form1()
        {
            InitializeComponent();
            ofd.InitialDirectory = "C:\\ProgramData";
        }    
        private void button1_Click(object sender, EventArgs e)
        {                     
          DialogResult dr = ofd.ShowDialog();
          ofd.InitialDirectory = null;   
        }    
    }
like image 68
Vinod Avatar answered Jan 29 '26 22:01

Vinod