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

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.

Related

how can I get Image address or image URL while selecting an image using C# [duplicate]

is there a way to get the image directory path after selecting in file dialog
example C:\Users\Admin\Desktop\IMG\pix200.jpg
i have a code that will open file dialog then after selecting Image and displaying Image in PictureBox
i need to get the image location or path directory as i said earlier
private void student_Edit_PictureBox_Front_DoubleClick(object sender, EventArgs e)
{
OpenFileDialog openFD = new OpenFileDialog();
openFD.Filter = "Bitmaps|*.bmp|jpeg|*.jpg";
if (openFD.ShowDialog() == DialogResult.OK)
{
student_Picture_Edit.Image = Bitmap.FromFile(openFD.FileName);
student_Edit_PictureBox_Front.Image = Bitmap.FromFile(openFD.FileName);
//I want to get the directory path Picturebox.Imagelocation is not working for me
}
}
is there a solution for this?
Try to make use of the Path.GetDirectoryName method.
Example:
string path = Path.GetDirectoryName(openFD.FileName);

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 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.

".RestoreDirectory" does not work

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.

Categories

Resources