Solved

Remebering the last saved directory path in a saveFileDialog

Posted on 2006-07-23
7
732 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 29

Accepted Solution

by:
anarki_jimbel earned 100 total points
Comment Utility
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_
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
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
Comment Utility
anarki, I think you got the names wrong....
0
 

Expert Comment

by:dreckgos
Comment Utility
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 29

Expert Comment

by:anarki_jimbel
Comment Utility
My apologies, dreckgos!

The message was to BBK.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now