C# FileDialogs aren't localized - c#

I have a OpenFileDialog and a SaveFileDialog in my C# program. However, they are always shown at English instead of Windows language.
I want them to be shown at Windows current language, like other programs do.
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string fileName = dialog.FileName;
//Process file...
}

Related

Get Handler to SaveFileDialog opened by Application

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

Creating and saving files in C#

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");
}

Open file Dialog issue

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

How can I set OpenFileDialog to accept http:// as well as local files?

For a C# Forms app, I want to allow OpenFileDialog to return urls as well as local and network filenames. How can I set it to do that?
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = FormStrings.Framework_rdlAddFileTitle;
dlg.CheckFileExists = true;
dlg.DefaultExt = "rdlx";
dlg.Filter = #"RDL(X) files|*.rdlx;*.rdl|RDLX files|*.rdlx|RDL files)|*.rdl|All files|*.*";
dlg.Multiselect = false;
if (dlg.ShowDialog(GetParentHWnd(doc)) != DialogResult.OK)
return;
Doesn't look like OpenFileDialog supports it -- MSDN.
I would create a textbox for the URL that contains the file. Then parse and fetch it in your code.

allowing only pdf files to be uploaded

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.

Categories

Resources