".RestoreDirectory" does not work - c#

In the following code I set ofd1.RestoreDirectory as false however, the dialog opens the inital directory everytime. Is there something that I am not aware of?
private void btnMeshFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd1 = new OpenFileDialog();
ofd1.Title = "Open";
ofd1.InitialDirectory = #"c:\";
ofd1.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
ofd1.FilterIndex = 2;
ofd1.RestoreDirectory = false;
if (ofd1.ShowDialog() == DialogResult.OK)
{
string fileName = Path.GetFileName(ofd1.FileName);
MeshDirectoryPath = Path.GetFullPath(ofd1.FileName).Replace(#"\", #"\\");
txtMeshFile.Text = fileName;
}
}

From MSDN documentation of RestoreDirectory
Gets or sets a value indicating whether the dialog box restores the
current directory before closing.
So this property is about restoring OS current directory.
But you, in the code also use InitialDirectory property, forcing the dialog every time start from #"c:\"; path. Remove this, and it will resolve your problem.

Related

If initial directory doesn't exist, create it, but if user cancel the save, delete the newly added folders

I want to save a textbox text to a .txt file created in a special directory, using a SaveFileDialog.
But let's say my path doesn't exist: I would like that when the dialogbox shows up to ask where the user want to save his .txt file, the dialogbox create automatically the missing folders if they are missing. But I also like that if the user cancel his saving, the newly created folder to erase if they are empty.
In other words: the SaveFileDialog dialogbox shows up in an initial directory, but if this initial directory is null, my code generate this directory BUT if the user cancel, my code erase the generated directory.
Here's my example: I want to save my .txt in Desktop\FolderExistingOrNot, but if the folder FolderExistingOrNot I want to creat it. But if the user cancels, I want to delete if FolderExistingOrNot is empty.
private void btn_SAVE_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "txt";
sfd.Filter = ".TXT (*.txt)|*.txt";
sfd.FileName = textBox1.Text;
sfd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\FolderExistingOrNot";
//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
{
// I would like to erase the folder FolderExistingOrNot if it's empty
}
}
It might be simple but I haven't figured out how to do it.
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);
}
}

Openfiledialog filter doesn't restrict files to open by typing full file path in File name text box C# winform

I am using the OpenFileDialog to open some specific file type, to do that I set the Filter property of OpenFileDialog. The filter setting does only hiding the file which are out of filter extensions, but user can still select those files by typing the file name in OpenFileDialog 'File Names:' text box.
I want user should not be able to select the file which are out of filter settings even typing the path manually in openFileDialog box 'File Names:' textbox.
This is the code.
private void button7_Click(object sender, EventArgs e)
{
EncryptFile();
}
public void EncryptFile()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Office Files|*.doc;*.xls;*.ppt";
dialog.InitialDirectory = #"C:\";
dialog.Title = "Please select an office file to encrypt.";
string fileName = "";
if (dialog.ShowDialog() == DialogResult.OK)
{
fileName = dialog.FileName;
/// doing something
}
}

Can we choose a particular file using folderbrowser dialogue and then copy the path of that file with its name to a text box

This click event open the folderbrowser dialog.. but my customer wants to watch only a particular text file in the folder to be watched by filewatcher.. and that path of the file should be written to the text box in the form
private void BrowserBtn_Click(object sender, EventArgs e)
{
//string startingDir = this.txtBoxPath.Text;
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.SelectedPath = this.txtBoxPath.Text;
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
if (!string.IsNullOrEmpty(dlg.SelectedPath))
{
this.txtBoxPath.Text = dlg.SelectedPath;
}
}
if (string.IsNullOrEmpty(this.txtBoxPath.Text))
{
this.txtBoxPath.Text = appDataFolder;
}
if (!Directory.Exists(path))
{
MessageBox.Show("Specified folder does not exist");
}
}
I serched trough internet.. but i couldnt find the way to select the files in the folder.. or should i use Openfiledialogue? doesnt open file dialogue opens the file? i dont wanna use it coz i dont want to open the file but just watch the changes in the selected file..
The files aren't open at this stage:
using (OpenFileDialog ofd = new OpenFileDialog())
{
if( DialogResult.OK == ofd.ShowDialog(this))
{
//Do something with the following files:
//ofd.FileNames
}
}
Or if you want to process a single file only you can use the FileName property. Files are not yet opened until you do something with those file(s).

How to set default path, then I press ShowDialog button in WPF?

So currently I get user decided path to file this way:
private void Button_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".xml";
dlg.Filter = "XML files (*.xml)|*.xml";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
// Open document
string xmlFile = dlg.FileName; // this required full path.
}
}
It works fine, but usually default path is .exe locating folder and need to change it. How can I do it?
If you are wanting to have the dialog open to a specific directory when .ShowDialog() is called you can set the InitialDirectory property to whatever path pleases you.
When you do this it's good practice to set the OpenFileDialog to null when you are finished.

How to make a selected path to save an XML file?

I have a function that can save a .xml file.
private void buttonSaveXML_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "XML Files|*.xml";
saveFile.Title = "Save a Xml File";
saveFile.ShowDialog();
if (saveFile.FileName != "")
{
FileStream fs = (FileStream)saveFile.OpenFile();
dsVersions.WriteXml(fs);
}
}
What do I add to make a specific path that I want to save it to?
You can control the InitialDirectory so that the user will be "in the right place", but you cannot prevent them from switching directories with SaveFileDialog.
That way, they will be in your default path rather than e.g. on the Desktop.
saveFile.InitialDirectory = #"C:\My\Path" ;
Typically I will save the last directory that the user selected to save files to in application configuration and use the user's last directory as the InitialDirectory.
Use the property InitialDirectory of SaveFileDialog form. For Example add this to your code:
saveFile.InitialDirectory = "C:\\MyXMLs\\";
You can see the Documentation.

Categories

Resources