Click to See Complete Forum and Search --> : How do I run an external process from C#?
meshman
October 6th, 2008, 09:45 AM
I'm trying to open an explorer window from within a C# program (or launch anything externally like a document or MP3).
I have this in my code, under a context menu selection:
Process.Start("explorer.exe", "C:\\");
The program runs fine and I select this item from the context menu. The explorer window opens no problem and then the program stops in program.cs on this line:
Application.Run(new Form1());
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
I've tried many different examples from simple to verbose and the program crashes AFTER the window has been opened (or document loaded or song played)
Is there something I'm missing?
Thanks!
Arjay
October 6th, 2008, 11:16 AM
Is there something I'm missing?Yes, posting the relevant code. The process code you posted just opens an explorer window that points to the c: drive, yet you mention playing a song. We can't help you unless you post the actual code you are using.
hspc
October 6th, 2008, 03:19 PM
meshman, the code you posted is OK. Seems that the exception is caused by the next code.
Please post compete code to help.
meshman
October 6th, 2008, 04:28 PM
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
int Flag = 0;
switch (e.ClickedItem.Text) {
case "Open Folder":
Process.Start("explorer.exe", Path.GetDirectoryName(dataGridView1.Rows[0].Cells[6].Value.ToString()));
break;
case "Play":
Process.Start("explorer.exe", dataGridView1.Rows[0].Cells[6].Value.ToString());
break;
case "Add to Playlist":
// Code for that...
break;
case "Remove From Playlist":
// Code for that...
break;
case "Open Playlist":
// Code for that...
break;
}
}
I've stepped through this line by line and every time, the explorer window opens (or the file is launched in winamp), I get to the end of the above and control goes back to the app no problem. Run it without breakpoints and it crashes every time. There is no following code, none of the other cases are accidentally executed.
I don't get why it crashes in full run but won't shen stepped through. any thoughts?
Thanks
(Sorry about the lack of indentation, it just posts that way)
Arjay
October 6th, 2008, 06:11 PM
You can get the code to indent if you use code tags (i.e. [ CODE] [/ CODE] minus the spaces).
You really should be validating this line.
dataGridView1.Rows[0].Cells[6].Value.ToString())
If the row or cell indexes are wrong or if the Value is null, you'll get an exception.
Another option is to put the switch statement inside a try/catch block.
EX:
try
{
switch( e.ClickItem.Text )
{
}
}
catch( Exception ex )
{
Debug.WriteLine( ex.Message );
}
meshman
October 9th, 2008, 07:49 AM
"You really should be validating this line."
I've tried a hard coded path:
Process.Start("explorer.exe", "C:\\");
and the problem still occurs.
???
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.