views:

637

answers:

2

Hello all:

App.config:

<add key="SaveDraftPath" value="C:\Drafts\"/>

C#:

var saveDraftPath = ConfigurationManager.AppSettings["SaveDraftPath"]; 
var sfDialog = new SaveFileDialog();
sfDialog.InitialDirectory = saveDraftPath;
sfDialog.FileName = "FILE";

For some reason this doesn't open the filebrowser in the path like planned, any one know why or how to fix?

I've tried this now, still doesn't work:

var saveDraftPath = Path.GetFullPath(ConfigurationManager.AppSettings["SaveDraftPath"]);
MessageBox.Show("does directory exist : " + Directory.Exists(saveDraftPath));
var sfDialog = new SaveFileDialog();
sfDialog.InitialDirectory = saveDraftPath;
sfDialog.FileName = "FILE";

and Directory.Exists(saveDraftPath) returns true.. Hmmm?!

Edit: The above code has worked once for me. The code works for everyone who has so far answered. But it is still not working. So I suspect the problem is some sort of local/history setting stopping it. Does anyone know why this might happen?

A: 

Try this:

var path = Path.GetFullPath(ConfigurationManager.AppSettings["SaveDraftPath"])

Have a look at Path Class as well, has got several helpful methods

Asad Butt
see edit, strangely still not working! very weird.
baron
A: 

this worked for me (getting the correct path from the config)

var saveDraftPath =
           ConfigurationManager.AppSettings["SaveDraftPath"];
        var sfDialog = new SaveFileDialog();
        sfDialog.InitialDirectory = saveDraftPath;
        sfDialog.FileName = "FILE";

        if (sfDialog.ShowDialog() == DialogResult.OK)
        {
            //do stuff
        }

see for more http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx

bluevoodoo1