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.
Related
EDIT: The main issue when renaming the file is to rename xaml contents
My intuition is screaming that there is a better way to do this, but the following works, my question is to see if there a better or an already existing method that can do this
I have a small WPF app that loads xaml files and can be imported/edited and exported.
When the WPF app initialises it makes a placeholder xaml file that you can edit,
"StartingXamlFile.xaml" when exporting I used a SaveFileDialog and the user can change the name. But the contents of the XAML file is not changed according to the file name. Also when importing an existing XAMLfile then editing and exporting(changing name when exporting) the same thing occurs.
Solution:
I set up a Singleton object to hold the file name XamlFile with property content and path. Now the default is set to "StartingXamlFile" when importing this is replaced. This is done following Gang of four Singleton technique
SaveFile code that doesnt work:
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "StartingXamlFile"; // Default file name
dlg.DefaultExt = ".xaml"; // Default file extension
dlg.Filter = "xaml files (*.xaml)|*.xaml|All files (*.*)|*.*"; // Filter files by extension
// Show save file dialog box
var result = dlg.ShowDialog();
if (result == true)
{
File.WriteAllText(dlg.FileName, editedContent);
}
Save File code that does work using Replace:
if (result == true)
{
var name = Path.GetFileNameWithoutExtension(dlg.FileName);
var testing = XamlFile.Content.Replace(XamlFile.Path,name);
File.WriteAllText(dlg.FileName, testing);
}
Just to give this question an accepted answer, I have not found a better way to this other than:
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "StartingXamlFile"; // Default file name
dlg.DefaultExt = ".xaml"; // Default file extension
dlg.Filter = "xaml files (*.xaml)|*.xaml|All files (*.*)|*.*";
// Show save file dialog box
var result = dlg.ShowDialog();
if (result == true)
{
//X:Class replacement is done here
var name = Path.GetFileNameWithoutExtension(dlg.FileName);
var testing = XamlFile.Content.Replace(XamlFile.Path,name);
File.WriteAllText(dlg.FileName, testing);
}
XamlFile is a singleton object that contains the content of the xaml
and initial name of file when starting up or importing, so when you export it
replaces all references to the initial name
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
I need to create and write to a .dat file. I'm guessing that this is pretty much the same process as writing to a .txt file, but just using a different extension.
In plain english I would like to know how to:
-Create a .dat file
-Write to it
-And save the file using SaveFileDialog
There are a few pages that I've been looking at, but I think that my best explanation will come from this site because it allows me to state exactly what I need to learn.
The following code is what I have at the moment. Basically it opens a SaveFileDialog window with a blank File: section. Mapping to a folder and pressing save does not save anything because there is no file being used. Please help me use this to save files to different locations.
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "";
dlg.DefaultExt = "";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
}
Pages that I've been looking at:
-http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx
-http://social.msdn.microsoft.com/Forums/en-US/cd0b129f-adf1-4c4f-9096-f0662772c821/how-to-use-savefiledialog-for-save-text-file
-http://msdn.microsoft.com/en-us/library/system.io.file.createtext(v=vs.110).aspx
Note that the SaveFileDialog only yields a filename but does not actually save anything.
var sfd = new SaveFileDialog {
Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*",
// Set other options depending on your needs ...
};
if (sfd.ShowDialog() == true) { // Returns a bool?, therefore the == to convert it into bool.
string filename = sfd.FileName;
// Save the file ...
}
Use the filename you are getting from the SaveFileDialog and do the following:
File.WriteAllText(filename, contents);
That's all if you intend to write text to the file.
You can also use:
File.WriteAllLines(filename, contentsAsStringArray);
using(StreamWriter writer = new StreamWriter(filename , true))
{
writer.WriteLine("whatever your text is");
}
I have set Multiselect to true in order to be able to load several files at once. The problem is that it ignores the order in which the user selects the files, the list of FileNames is always the same (if I select the same set of files in different order).
My question is: is it possible to achieve the desired behavior? (Store paths in the same order as selected).
List<string> filePaths = new List<string>();
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Multiselect = true;
dlg.DefaultExt = ".txt";
// dlg.Filter = my filter //
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
foreach (string file in dlg.FileNames)
{
}
}
Is this help?
open file dialog multislection order problem
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.