Setting the initial directory of an SaveFileDialog?
You need to set the RestoreDirectory
to true
as well as the InitialDirectory
property.
SaveFileDialog InitialDirectory
Have a look at this article about String literals
You should either use double back slashes if you want one backslash
saveFileDialog1.InitialDirectory = "W:\\etc\\etc\\";
or use the @ sign:
saveFileDialog1.InitialDirectory = @"W:\etc\etc\";
Also you should set the RestoreDirectory in order to set the working directory back to your application directory.
Check this link for reference
saveFileDialog1.RestoreDirectory = true;
SaveFileDialog Initial Directory Doesnt work C# Visual Studio
You are setting initial directory for sfd instance of SaveFileDialog
which you are creating locally:
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = @"C:\\";
But you are opening saveFileDialog1 instance which is form's field:
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
Of course, it uses default initial directory. You should either change settings of saveFileDialog1
or use sfd.ShowDialog()
Validating InitialDirectory for SaveFileDialog?
The quick and easy way to fix it would be to get the full path:
dialog.InitialDirectory = Path.GetFullPath(initialDirectory);
This will expand relative paths to the absolute ones that the SaveFileDialog
expects. This will expand just about anything that resembles a path into a full, rooted path. This includes things like "/" (turns into the root of whatever drive the current folder is set to) and "" (turns into the current folder).
How do I set the Initial Directory on an OpenFileDIalog to the users `Downloads` folder in C#
Maybe this could help: https://stackoverflow.com/a/1175250/333404
UPDATE:
Works for me:
https://stackoverflow.com/a/3795159/333404
private void Button_Click_1(object sender, RoutedEventArgs e) {
var ofd = new OpenFileDialog();
ofd.InitialDirectory = GetDownloadsPath();
ofd.Filter = "Zip Files|*.zip";
ofd.ShowDialog();
}
public static string GetDownloadsPath() {
string path = null;
if (Environment.OSVersion.Version.Major >= 6) {
IntPtr pathPtr;
int hr = SHGetKnownFolderPath(ref FolderDownloads, 0, IntPtr.Zero, out pathPtr);
if (hr == 0) {
path = Marshal.PtrToStringUni(pathPtr);
Marshal.FreeCoTaskMem(pathPtr);
return path;
}
}
path = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
path = Path.Combine(path, "Downloads");
return path;
}
private static Guid FolderDownloads = new Guid("374DE290-123F-4565-9164-39C4925E467B");
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern int SHGetKnownFolderPath(ref Guid id, int flags, IntPtr token, out IntPtr path);
If initial directory doesn't exist, create it, but if user cancel the save, delete the newly added folders
This worked for me when I tested it.
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "txt";
sfd.Filter = ".TXT (*.txt)|*.txt";
sfd.FileName = textBox1.Text;
string mypath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\FolderExistingOrNot";
Directory.CreateDirectory(mypath);
sfd.InitialDirectory = mypath;
//Directory.CreateDirectory(sfd.InitialDirectory); // could use that but if the user cancel, this folder will not be destroyed
if (sfd.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfd.FileName, textBox2.Text);
}
else // if the user cancel the saving
{
if (Directory.GetFiles(mypath).Length == 0)
{
Directory.Delete(mypath);
}
}
How to set a path for savefiledialog
You have to set InitialDirectory
before you're showing your dialog.
SaveFileDialog1.InitialDirectory = "C:\Users\owner\Downloads"
SaveFileDialog1.ShowDialog()
Find out last directory of SaveFileDialog
The path comes from the registry -
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Comdlg32\LastVisitedPidlMRU
or for older Windows OSHKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Comdlg32\OpenSaveMRU
Also note what @Jimi mentioned about the RestoreDirectory
property.
Edit: I had initially thought that the regkey/value was just a unicode string but it is not so straight forward to use. So reconsider this approach. If you really need to figure out how the keys work I suggest you take a look at - https://github.com/aelij/svcperf/blob/master/src/Viewer/UIUtils/MruFileHelper.cs
Related Topics
How to Make Correct Date Format When Writing Data to Excel
Comparing 2 Objects and Retrieve a List of Fields with Different Values
Synchronizationlockexception on Monitor.Exit When Using Await
How to Avoid a System.Runtime.Interopservices.Comexception
How to Make a Console Application Run Using Only a Single File in .Net Core
Is an Int a 64-Bit Integer in 64-Bit C#
What's the Quickest Way to Compute Log2 of an Integer in C#
How to Use Xpath Function in a Xpathexpression Instance Programatically
Fastest Way to Remove White Spaces in String
Accessing a Property of Derived Class from the Base Class in C#
How to Pass Objects into an Attribute Constructor
C# Form.Transparencykey Working Different for Different Colors, Why
How to Know the Repeating Decimal in a Fraction
Generic Methods and Method Overloading
Inconsistent Accessibility Error with the Following C# Code. Why