Solved

Remebering the last saved directory path in a saveFileDialog

Posted on 2006-07-23
7
760 Views
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.

Ramesh
0
Comment
Question by:Ramesh4u
  • 3
  • 3
7 Comments
 
LVL 30

Accepted Solution

by:
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
  tr.Close();
 
  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
  tw.WriteLine(path);

  // close the stream
  tw.Close();

}
0
 
LVL 3

Expert Comment

by:BBK_
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
0
 

Expert Comment

by:dreckgos
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:

dialog.FileName=@"C:\";
the "@" specifies not to interpret the \ as an escape sequence.

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 30

Expert Comment

by:anarki_jimbel
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 http://www.knowdotnet.com/articles/registrywrapper.html
0
 

Expert Comment

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

Expert Comment

by:dreckgos
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!
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 17179979
My apologies, dreckgos!

The message was to BBK.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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