OpenFileDialog.ShowDialog() raises InvalidOperationException in Silverlight - c#

Below is my code snippet.
OpenFileDialog dialog = new OpenFileDialog()
{
Filter = "Excel Files (*.xlsx;*.xls;)|*.xlsx;*.xls;",
};
if (dialog.ShowDialog().Value == false)
{
IsCommiting = false;
return;
}
else
{
Items.Clear();
}
When dialog.ShowDialog() called, InvalidOperationException was raised with this message "ShowDialog failed.".
I've found a related article by google search.
http://social.msdn.microsoft.com/Forums/en-US/silverlightmvvm/thread/6ae9454b-b5ba-4286-959f-6dc4d347ebf7/
This article is about multi-selection in open file dialog. But I didn't set Multiselect property to True. This problem doesn't happen often, and cannot be reproduced.
Is there anyone who suffered from same problem?

Can you try:
OpenFileDialog dialog = new OpenFileDialog()
{
Filter = "Excel Files (*.xlsx,*.xls)|*.xlsx,*.xls"
};
if (dialog.ShowDialog() == false)
{
IsCommiting = false;
return;
}
else
{
Items.Clear();
}

Though it may sound a bit strange - since OpenFileDialog actually opens an Explorer window (which executes shell extensions), have you considered, disabling all shell extensions by ShellExView?

Try the following:
if (dialog.ShowDialog() != DialogResult.OK)

Related

OpenFileDialog is not opening the file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have bound the openFileDialog control with button. On button event, I am calling the openFileDialog control.
Now, when I am running my application, the openFileDialog box gets open but does not select the file. The open button is not working.
Example code:
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk_1(object sender, CancelEventArgs e)
{
// logic written here
}
It was working fine earlier but now its not working.
You need to use the DialogResult to get the event of open confirmation by the user. Then you can use a stream to read the file. Here is some sample code (provided by MS in the MSDN - source:https://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog(v=vs.110).aspx):
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Some logic here
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Failed to open file. Original error: " + ex.Message);
}
}
}
Answer post edits
The included code shows the method openFileDialog1_FileOk_1 the "_1" at the end suggest to me that you had some issues binding the event. Perhaps there was at some point a method openFileDialog1_FileOk caused conflict.
You should check if the method is correctly bound to the event.
For that I will remit you to my answer to How to change the name of an existing event handler?
For abstract you want to see what method is bound to the event. You can do it from the properties panel, or by checking the form designer file (that is named something .Designer.cs for example: Form1.Designer.cs).
Addendum: Consider adding a break point in the event handler. It will allow you to debug step by step what is happening. Also, it will allow you notice if the event handlers is not being executed at all, which would suggest that the method is NOT correctly bound to the event.
Original Answer
OpenFileDialog Does not open files, it barely select thems and makes the selection available for your application to do whatever your applications is intended to do with those files.
The following is the usage pattern from the MSDN article linked above:
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show
(
"Error: Could not read file from disk. Original error: " + ex.Message
);
}
}
Now, observe that after cheking the result of ShowDialog - which returns once the dialogs is closed - the codes uses the method OpenFile to actually open the file. The result is a stream that you can process anyway you prefer.
Alternatively you can retrieve the selected files via the property FileNames, which returns an array of strings. If you have configured the dialog to only allow selecting asingle file you are ok to use FileName instead.
Addendum, if by "Open" you mean to invoke the default application associated with the selected file to open the file. You can accomplish that by passing the file path to System.Diagnostics.Process.Start.
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string name = openFileDialog1.FileName;
object objOpt = Type.Missing;
var excelApp = new Excel.Application();
Excel.Workbook wbclass = excelApp.Workbooks.Open(name, objOpt,
true,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt);
}

Opening text files in C# application

I'm still a beginner when it comes to programming and this is a small application I did following a C# tutorial.
private void viewImagesToolStripMenuItem_Click(object sender, EventArgs e)
{
string openedfile = "";
openfd.Title = "Insert a text file";
openfd.InitialDirectory = "C:";
openfd.FileName = "";
openfd.Filter = "text files|*.txt|word documents|*.doc|allfiles|*.*";
if (openfd.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Operation canceled");
}
if (openfd.ShowDialog() != DialogResult.Cancel)
{
openedfile = openfd.FileName;
richTextBox1.LoadFile(openedfile,RichTextBoxStreamType.PlainText);
}
While doing this I noticed that if I change the same application's code order just 2 lines-
string openedfile = "";
openedfile = openfd.FileName;
like below It will throw me an error like this when debugging - Empty path name is not legal.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openfd.Title = "Insert a text file";
openfd.InitialDirectory = "C:";
openfd.FileName = "";
openfd.Filter = "text files|*.txt|word documents|*.doc|allfiles|*.*";
**string openedfile = "";
openedfile = openfd.FileName;**
if (openfd.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Operation canceled");
}
if (openfd.ShowDialog() != DialogResult.Cancel)
{
richTextBox1.LoadFile(openedfile,RichTextBoxStreamType.PlainText);
}
Isn't there a way to understand errors in these type situations. What is the specific order of coding an application like this?
well, the idea is simple you cannot use a variable that has been not initialized.
in your case sm thing same is happening.
In your first code openedfile = openfd.FileName; is being executed after the dialouge has been shown. Thus the file name comes correctly.
But in the second openedfile = openfd.FileName; is getting initialized before the dilogue has been shown. Since there is no dialogue the name is null, hence it gives error.
Note. I have used initialized word not in tecnical manner.
The line openedfile = openfd.FileName; will not bind the two variables, it will copy the value that openfd.FileName has at this moment into openedfile.
In your second example the user has not yet selected a file at that moment, so this value is still empty (""). The value that is selected later in openfd will be ignored.
EDIT which is why you get the error Empty path name is not legal.
I'm going to go ahead and guess that the problem is the openfd.FileName call outside of the if block (and also before its retrieved), while the if block is still being exectuted the openfd is "left open" if you like, so you can retrieve its result.
When you have left, the if block, you are effectively saying you are done with this dialog, please continue.
In your code you are showing multiple dialogs with multiple calls to show dialog also, consider the following.
if (openfd.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openfd.FileName);
}
else
{
MessageBox.Show("Operation canceled");
}
(Changed to use dialogresult.ok as this is more than likely the result you wish to receive from the dialog)
Update
With respect to your current applcation, each call to ShowDialog() is opening a new dialog. Consider it similar to
MessageBox.Show("woo");
MessageBox.Show("hoo");
in the above, when the first messagebox is closed, it will close the dialog and move onto handling the second message box (the next line of code), with your
if (openfd.ShowDialog() != DialogResult.Cancel)
Your showdialog is still in use by the if statement so it is deemed to be still in use and not disposed of straight away. When the if statement is finished with, your dialog will then be deemed to be ok to dispose
Also, the error in your application is not to do with the filename path, its trying to load in a file that has no name
I generally Prefer this:
private void viewImagesToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult dr=openfd.ShowDialog();
if(dr==DialogResult.Ok)
{
richTextBox1.LoadFile(openfd.FileName,RichTextBoxStreamType.PlainText);
}
else
{
MessageBox.Show("No file Selected!!");
}
}

Implementing an OpenFileDialog with sound file preview

I would like to implement an open file dialog or file browser that additionally offers a "Preview" button to play the currently selected sound file (wave format in particular, other formats are not necessary for this application).
I could create my own form with various controls such as a treeview and listbox to show the folders and files, but I think I would be reinventing the wheel, or if nothing else going to a lot of work for something very simple. Do you recommend doing this?
Can I modify (inherit) the existing OpenFileDialog and add the sound-playing button to it somehow?
Is there some free library of custom file pickers that could be utilized? (Provided that the license allows inclusion in a commercial sense.)
Before you get carried away hacking the dialog, consider a simple solution first that leverages the FileOk event. Create a form named, say, frmPreview. Give it a constructor that takes a string. You'll need a Cancel and an OK button and code to play the file.
Display that form like this:
var dlg = new OpenFileDialog();
// Set other dlg properties...
dlg.FileOk += (s, cancel) => {
using (var prev = new frmPreview(dlg.FileName)) {
if (prev.ShowDialog() != DialogResult.OK) cancel.Cancel = true;
}
};
if (dlg.ShowDialog(this) == DialogResult.OK) {
// use the file
//...
}
Now, whenever the user clicks Open, your preview form shows up. The user can click Cancel and pick another file from the dialog.
Found this question whilst searching before asking my own. Possible slight simplification of Hans' answer is to use a standard Message Box rather than having to write your own form. Still a popup on a popup though.
private void btnSelect_Click(object sender, RoutedEventArgs e) {
var dlg = new Microsoft.Win32.OpenFileDialog {
DefaultExt = ".csv",
Filter = "Wav Files Only (*.wav)|*.wav",
InitialDirectory = "C:\\Windows\\Media\\",
CheckFileExists = true
};
dlg.FileName = "preselect the existing file if you wish";
dlg.FileOk += (s, cancel) => {
var player = new MediaPlayer();
player.Open(new Uri(dlg.FileName));
player.Play();
var msgres = MessageBox.Show(Path.GetFileName(dlg.FileName)+"\nUse this sound?", "Sound Playing", MessageBoxButton.YesNo);
if (msgres != MessageBoxResult.Yes) cancel.Cancel = true;
player.Stop(); //in case it is a long sound
};
var result = dlg.ShowDialog();
if (result != true) return;
//do whatever with dlg.FileName ...
}
Using a MessageBox provides a clean standard interface
Regarding point 2, I had thought the OpenFileDialog (or SaveFileDialog) weren't extendable in any way - they are provided by the OS.
But, it turns out they could be:
http://www.codeproject.com/KB/dialog/WPFCustomFileDialog.aspx
http://www.codeproject.com/KB/dialog/CustomizeFileDialog.aspx
The first one looks like what you're wanting to achieve.
Good luck.

System.Windows.Forms.SaveFileDialog does not enforce default extension

I am trying to make SaveFileDialog and FileOpenDialog enforce an extension to the file name entered by the user. I've tried using the sample proposed in question 389070 but it does not work as intended:
var dialog = new SaveFileDialog())
dialog.AddExtension = true;
dialog.DefaultExt = "foo";
dialog.Filter = "Foo Document (*.foo)|*.foo";
if (dialog.ShowDialog() == DialogResult.OK)
{
...
}
If the user types the text test in a folder where a file test.xml happens to exist, the dialog will suggest the name test.xml (whereas I really only want to see *.foo in the list). Worse: if the user selects test.xml, then I will indeed get test.xml as the output file name.
How can I make sure that SaveFileDialog really only allows the user to select a *.foo file? Or at least, that it replaces/adds the extension when the user clicks Save?
The suggested solutions (implement the FileOk event handler) only do part of the job, as I really would like to disable the Save button if the file name has the wrong extension.
In order to stay in the dialog and update the file name displayed in the text box in the FileOk handler, to reflect the new file name with the right extension, see the following related question.
You can handle the FileOk event, and cancel it if it's not the correct extension
private saveFileDialog_FileOk(object sender, CancelEventArgs e)
{
if (!saveFileDialog.FileName.EndsWith(".foo"))
{
MessageBox.Show("Please select a filename with the '.foo' extension");
e.Cancel = true;
}
}
AFAIK there's no reliable way to enforce a given file extension. It is a good practice anyway to verify the correct extension, once the dialog is closed and inform the user that he selected an invalid file if the extension doesn't match.
The nearest I've got to this is by using the FileOk event. For example:
dialog.FileOk += openFileDialog1_FileOk;
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
if(!dialog.FileName.EndsWith(".foo"))
{
e.Cancel = true;
}
}
Checkout FileOK Event on MSDN.
I ran into this same issue, and I was able to control what was shown by doing the following:
with the OpenFileDialog, the first item in the filter string was the default
openFileDialog1.Filter = "Program x Files (*.pxf)|*.pxf|txt files (*.txt)|*.txt";
openFileDialog1.ShowDialog();
with the SaveFileDialog, the second item in the filter was used as the default:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|Program x Files (*.pxf)|*.pxf";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog1.FileName != null)
{
// User has typed in a filename and did not click cancel
saveFile = saveFileDialog1.FileName;
MessageBox.Show(saveFile);
saveCurrentState();
}
}
After having used these two filters with the respective fileDialogs, The expected results finally occurred. By default, when the user selects the save button and the savefiledialog shows up, the selected filetype is that of the Program X files type defined in the filter for the savefiledialog. Likewise the selected filetype for the openfiledialog is that of the Program X Files Type defined in the filter for the openfileDialog.
It would also be good to do some input validation as mentioned above in this thread. I just wanted to point out that the filters seem to be different between the two dialogs even though they both inherit the filedialog class.
//this must be ran as administrator due to the change of a registry key, but it does work...
private void doWork()
{
const string lm = "HKEY_LOCAL_MACHINE";
const string subkey = "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoComplete";
const string keyName = lm + subkey;
int result = (int)Microsoft.Win32.Registry.GetValue(keyName, "AutoComplete In File Dialog", -1);
MessageBox.Show(result.ToString());
if(result.ToString() == "-1")
{
//-1 means the key does not exist which means we must create one...
Microsoft.Win32.Registry.SetValue(keyName, "AutoComplete In File Dialog", 0);
OpenFileDialog ofd1 = new OpenFileDialog();
ofd1.ShowDialog();
}
if (result == 0)
{
//The Registry value is already Created and set to '0' and we dont need to do anything
}
}

Making the second and third OpenFileDialog appear above the console in .NET/C#?

I'm writing a console program that can accept 1 to 3 files. I'm using OpenFileDialog three times to accept the files, but the second and third time the file dialog is behind the console window, making it hard to notice. Any way to get it to appear above?
An image of the problem:
The relevant code is:
static bool loadFile(ref List<string> ls)
{
OpenFileDialog f = new OpenFileDialog();
if (f.ShowDialog() == DialogResult.OK)
{
Console.WriteLine("Loaded file {0}", f.FileName);
ls.Add(f.FileName);
return true;
}
else
{
return false;
}
}
[STAThread]
static void Main(string[] args)
{
// sanity check
if (args.Length > 3)
{
Console.WriteLine("Sorry, this program currently supports a maximum of three different reports to analyze at a time.");
return;
}
else if (args.Length == 0)
{
List<string> fL = new List<string>();
for (int k = 0; k < 3; k++)
{
if (!loadFile(ref fL)) break;
}
if (fL.Count == 0)
{
InfoDisplay.HelpMessage();
return;
}
else
{
args = fL.ToArray();
}
}
// main program
...
}
I know this doesn't directly answer the question, but the OpenFileDialog has a property called "MultiSelect" which indicates whether or not the user can select more than one file. Once the user does select the file(s), the property FileNames (string[]) gets populated with all the file names. You can then just do a check like this:
if(dialog.FileNames.Length > 3)
{
//only 3 are allowed
}
I encountered the same problem in a console application.
OpenFileDialog.ShowDialog can be called with a "parent window" handle argument. I create a dummy Form and use that as parent window argument (the dummy form is invisible, since I don't call Show() on it).
The following code works for me:
Form dummyForm = new System.Windows.Forms.Form();
OpenFileDialog myOpenFileDialog1 = new OpenFileDialog();
if (myOpenFileDialog1.ShowDialog(dummyForm) == DialogResult.OK) {
fileName1 = myOpenFileDialog1.FileName;
}
OpenFileDialog myOpenFileDialog2 = new OpenFileDialog();
if (myOpenFileDialog2.ShowDialog(dummyForm) == DialogResult.OK) {
fileName2 = myOpenFileDialog2.FileName;
}
For me, dummy forms and label still didn't solve the problem. CodeProject has a partial solution at http://www.codeproject.com/KB/WPF/MessageBox.aspx
You can then use Process.GetCurrent.Process.MainWindowHandle in the constructor to the class in the article above, then pass the instance to the file dialog, and it seems to work well (for me at least).
Process p = Process.GetCurrentProcess();
WindowWrapper w = new WindowWrapper(p.MainWindowHandle);
OpenFileDialog ofd = new OpenFileDialog();
var ret = ofd.ShowDialog(w);
...
Found a similar post here. Not tested, but give it a shot and let me know.
foreach(Process p in Process.GetProcesses)
{
if(p.MainWindowTitle = <the main UI form caption>)
{
if(IsIconic(p.MainWindowHandle.ToInt32) != 0)
{
ShowWindow(p.MainWindowHandle.ToInt32, &H1);
ShowWindow(f.Handle.ToInt32, &H1);
}
else
{
SetForegroundWindow(p.MainWindowHandle.ToInt32);
SetForegroundWindow(f.Handle.ToInt32);
}
}
}
OpenFileDialog.ShowDialog can be
called with a "parent window" handle
argument. I create a dummy Form and
use that as parent window argument
(the dummy form is invisible, since I
don't call Show() on it).
The Form is a bit heavy-weight, perhaps a label instead, since it just takes a IWin32Window?
OpenFileDialog.ShowDialog(new Label());
It seems to work.
If someone could come up with a reasonable solution to this (now-) resurrected question, that would be great.
Aaron

Categories