Open file Dialog issue - c#

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

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

C# FileDialogs aren't localized

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

Bypassing save dialog box in WPF application

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

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