OpenFileDialog - InitialDirectory problem

Answered OpenFileDialog - InitialDirectory problem

  • 2007年8月20日 17:20
     
     
    I have a method that allows a user to open a file.  The InitialDirectory property is set at the beginning of the method.  If the user selects a file in a different folder, the next time the dialog opens, it opens to the last folder used insted of InitialDirectory folder.  How can I stop this?

    Thanks

    LTCA

すべての返信

  • 2007年8月20日 20:10
     
     回答済み
    Well I found a solution, but it's kind of kludgey.  In order to get the OpenFileDialog to 'forget' the last folder it was in and use the same folder everytime (in C#) you have to reset the object and then set all of the properties again.

    this.openFileDialog1.Reset();
    this.openFileDialog1.Multiselect = true;
    this.openFileDialog1.Filter = @"ACH files |*.ach|All Files |*.*";
    this.openFileDialog1.InitialDirectory = this.SourceFolder;

    LTCA
  • 2007年8月21日 7:10
     
     

    Before you call .ShowDialog on it, you only need to reset the initial directory, and not anything else.

     

    This should reset the InitialDirectory every time:

     

    this.openFileDialog1.InitialDirectory = this.SourceFolder;

     

  • 2007年8月21日 14:37
     
     
    Thanks for the reply.  Unfortunately this doesn't work.  A button on the form calls the dialog.  The first time I click it it opens to the source folder.  If I navigate to a different folder and open a file, the next time I click it it opens to the different folder.  It alwasys opens to the previous folder used.

     

  • 2007年8月22日 8:30
     
     

    My bad--I was trying it without really opening any file.  If you open a file, it behaves as you have said.

     

    It looks like you need this.openFileDialog1.Reset(); which is lame, because then you have to reset everything, as you have stated.   I see no way around this.

     

     

    -Matt-

     

  • 2008年9月18日 20:38
     
     
    I'm having the same problem but I create the dialog everytime the "button" is pushed and it worked the first time but never again. I even tried the reset but that doesn't work...

    I guess I'm still looking for a solution.
    tsfreaks
  • 2009年3月18日 0:01
     
     
    It is still broken.  Even if it is a "Feature", the inability to set the initial directory is a problem for anyone who has the desire or need to use multilple directories.  The reset solution does not work.  Dynamically generating the control at run time and disposing of it when done with the immediate use and regenerating it does not work.  Entering the registry to fix things is at best ptoblematic and at worst can be disasterous.

    As far as I am concderned this is NOT a feature , it is a bug that I hope can get fixed very soon or at least a workaround provided that works if you actually open a file.
  • 2010年6月9日 4:12
     
     回答の候補
    This is an old thread, but no one really resolved this so I'm responding.  It might be that the FileName property is interfering, if it's already set the dialog ignores the InitialDirectory.  So just set the filename to blank and then it picks up your new initialdirectory, no need to rebuild the whole thing from scratch every time.  Was driving me crazy too.
  • 2011年2月28日 14:56
     
     
    I faced with the issue. All found approaches didn't work for me - neither using dialog.Reset() nor dialog.FileName = string.Empty; Are there any more ideas/succeeded solutions around the reported issue? any advise would be appreciated.  
  • 2012年4月14日 13:45
     
     

    I couldn't understand your question well so I give 2 solution:

    OpenFileDialog.RestoreDialog =  true;

    OpenFileDialog.RestoreDialog =  false;