I am trying to get the handler to a SaveFileDialog which is opened by a button click of my WPF app. All the examples that I could find in the net actually creates one , but I need to handle one which is already opened. How do I do that?
The below code always creates a new SaveFileDialog
dlg.DefaultExt = "pdf"; // Default file extension
dlg.Filter = "PDF File (*.pdf)|*.pdf|All files (*.*)|*.*"; // Filter files by extension
dlg.FilterIndex = 2;
dlg.InitialDirectory = "C:\\Users\\Reema.Sinha\\Downloads";
Manager.Current.DialogMonitor.AddDialog(dlg);
DownloadSaveButton.Click();
DialogResult result = dlg.ShowDialog();
I guess OpenFileDialog can also do the trick. But I will prefer SaveFileDialog only. I tried this and hope it works for you also:
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.DefaultExt = "pdf"; // Default file extension
dlg.Filter = "PDF File (*.pdf)|*.pdf|All files (*.*)|*.*"; // Filter files by extension
dlg.FilterIndex = 2;
dlg.InitialDirectory = "C:\\Users\\Reema.Sinha\\Downloads";
Manager.Current.DialogMonitor.AddDialog(dlg);
DownloadSaveButton.Click();
Nullable<bool> result = dlg.ShowDialog(); // Show save file dialog box
if (result == true)
{
// Save document
string filename = dlg.FileName;
}
UPDATE:
Use FolderBrowserDialog. Then after you show the dialog to the user, you can do dlg.SelectedPath.
Hopefully these posts are helpfull to you.
SaveFileDialog that permits selection of folder
WPF select folder dialog
Open directory dialog
Related
I am trying to bypass the save dialog box when using the SaveFileDialog class. I want to be able to write to a document without having to prompt a user to decide if they want to save or not, the file should automatically save when they click a button.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.InitialDirectory = #"C:\";
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
// Code to write the stream goes here.
}
I have tried removing the if statement as well as using...
saveFileDialog1.CreatePRompt = false;
Nothing seems to work... Any ideas?
I think you want to bypass the overwrite prompt dialog.
In this case you can use
saveFileDialog1.OverwritePrompt = false;
Otherwise, you don't need a SaveFileDialog and you can save your stream without using it.
I found my answer. The question I asked was actually two it seems. The first was how to bypass SaveFileDialog, I wanted to use saveFileDialog because it can remember the last folder it accessed and opens to that folder when performing a read/save. That being said I implemented this...
Directory = System.AppDomain.CurrentDomain.BaseDirectory;
This will set Directory to the location of my executable.
Next I got rid of SaveFileDialog and just wrote to a file without ever prompting the user.
Thanks for all the pointers and ideas!
The end result ended up...
Directory = System.AppDomain.CurrentDomain.BaseDirectory;
using(System.IO.StreamWriter file = new System.IO.StreamWriter(Directory, false))
{
// File contents
}
Works perfectly for what I need it for.
If you want to save but you need the user to pick a filename, I would use this:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
//setup properties of Dialog
bool filenamepicked = false;
while (!filenamepicked)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//Save file
filenamepicked = true;
}
else
{
MessageBox.Show("You have to use a file name.");
}
}
I want to display an open file dialog and filter both .csv and .txt files. I've set the filter accordingly but it's only showing .csv files.
Here's my code:
var dialog = new Microsoft.Win32.OpenFileDialog();
dialog.Filter = "CSV files (*.csv)|*.csv|txt files (*.txt)|*.txt";
bool? isOK = dialog.ShowDialog();
if (isOK == true)
{
ImportFilePath = dialog.FileName;
}
If you want to show multiple file types at the same time, you should include them in a single filter:
dialog.Filter = "Plain text files (*.csv;*.txt)|*.csv;*.txt";
Otherwise the user will have to select the type of file in the bottom Combobox of the dialog.
Msdn has some useful examples of this.
I wrote below code for selecting only pdf files but it is not working.
OpenFileDialog fd = new OpenFileDialog();
fd.ShowDialog();
fd.Filter = "Pdf files (*.Pdf)|*.Pdf";
Please help is something wrong in this?
You need to set the Filter first before opening the dialog.
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "Pdf files (*.Pdf)|*.Pdf"; //this should be before
fd.ShowDialog();
Habib has the correct answer, but I felt I would add that you should check the response to ShowDialog to ensure that the user didn't cancel the dialog box. If they cancel the dialog box without selecting a file then the OpenFileDialog will say the file name is "", which is not going to be useful in the rest of your application.
Example
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "PDF Files(*.pdf)|*.pdf";
if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// Do stuff here
}
else
{
// The user cancelled the request to select a PDF
}
Hope this helps
In the below C# WPF code snippet, I want to load an XML document, edit the document, and save the output to a user designated location. I can use the XmlDocument.Save method to save to a pre-defined location, but how can I allow the user to save to any location like when choosing 'SaveAs'?
XmlDocument doc = new XmlDocument();
doc.Load(#"C:\OriginalFile.xml");
doc.Save("File.xml");
see the code below; be aware that the UAC if the user select some system folder.
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Xml (*.xml)|*.xml";
if (saveFileDialog.ShowDialog().Value)
{
doc.Save(saveFileDialog.FileName);
}
Use SaveFileDialog. Sample from the article:
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".xml";
dlg.Filter = "Xml documents (.xml)|*.xml"; // Filter files by extension
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
// Save document
string filename = dlg.FileName;
}
I have a browse button in my windows form application and i wanted to only filter down to the option of choosing pdf files. So in the browse file window only pdf files will be visible and not showing .doc or any kind of document format.
private void btnSelectFile_Click(object sender, EventArgs e)
{
var dlg = new OpenFileDialog();
var res = dlg.ShowDialog();
if (res == DialogResult.OK)
{
DocumentUNCPath.Text = dlg.FileName;
}
}
Firstly you need to apply a filter first to the OpenFileDialog such as:
dlg.Filter = "PDF Files|*.pdf";
However, that doesn't stop them from forcing through a file (which they can do). You can again check the filename again after they click on OK but this is no guarantee that the file you get will be a PDF.
To be safe you could use a PDF library either locally or server side to try and open the PDF file and see if it really is such.
Add this:
dlg.Filter = "PDF files|*.pdf";
You'll want to set the filter property on your dlg object like this:
var dlg = new OpenFileDialog();
dlg.Filter = "*.pdf";
var res = dlg.ShowDialog();
if (res == DialogResult.OK)
{
DocumentUNCPath.Text = dlg.FileName;
}
You want to use the Filter property of the OpenFileDialog.
dlg.Filter = "PDF Files|*.pdf"
The portion to the left of the | can be anything, I just gave you an example, but it's what's shown to the user. The portion to the right of the | is the actual Windows filter.