Remebering the last saved directory path in a saveFileDialog

Posted on 2006-07-23
Last Modified: 2008-01-09

Hi all,

I am implementing a saveFileDialog in C# . First time when I choose a directory to save a file, I want to remember it so that for the second time , I do not have to start from my C: drive and go to the previously saved directory.

There should be a easy way to do, and your help on this is appreciated.

Question by:Ramesh4u
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 30

Accepted Solution

anarki_jimbel earned 100 total points
ID: 17165107
It depends on what you call "an easy way"

I believe the easies way is to save the file (directory) path into a text or xml file.
If it's a text file you just write or read one line

public string readPath()
  // create reader & open file
  TextReader tr = new StreamReader("path.txt");// or (Application.StartupPath + "\\" + "path.txt")

  // read a line of text
  string path = tr.ReadLine();

  // close the stream
  return path;

You need to add error handling if no file exists (return "C:\")

public void writePath(string path)
  // create a writer and open the file
  TextWriter tw = new StreamWriter("path.txt"); // or (Application.StartupPath + "\\" + "path.txt")

  // write a line of text to the file

  // close the stream


Expert Comment

ID: 17165415
If you want to retain the selected directory name only till the application close, then better keep the value in a static variable,
If you want to keep this every time application opens, then better approch is to store the value in registry than in any XML or text file

Expert Comment

ID: 17165856
I think you want to find out how to set the dialog programmaticly to a specified path, am I right?

Just before showing the dialog, set the dialog.filename property to your desired directory, like this:

the "@" specifies not to interpret the \ as an escape sequence.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 30

Expert Comment

ID: 17172328
2 dreckgos

"then better approch is to store the value in registry than in any XML or text file"
No doubts. Give an example of code. Ramesh asked for an easy solution :)

2 Ramesh
If you want to save in the registry you may have a look at

Expert Comment

ID: 17173645
anarki, I think you got the names wrong....

Expert Comment

ID: 17173674
anarki, I think you got the names wrong....

 Dont forget Ramesh... save the directory in any way you want and then set the filename or filepath property!
LVL 30

Expert Comment

ID: 17179979
My apologies, dreckgos!

The message was to BBK.

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question