|
-
October 6th, 2008, 09:45 AM
#1
How do I run an external process from C#?
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!
-
October 6th, 2008, 11:16 AM
#2
Re: How do I run an external process from C#?
 Originally Posted by meshman
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.
-
October 6th, 2008, 03:19 PM
#3
Re: How do I run an external process from C#?
meshman, the code you posted is OK. Seems that the exception is caused by the next code.
Please post compete code to help.
-
October 6th, 2008, 04:28 PM
#4
Re: How do I run an external process from C#?
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)
-
October 6th, 2008, 06:11 PM
#5
Re: How do I run an external process from C#?
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.
Code:
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:
Code:
try
{
switch( e.ClickItem.Text )
{
}
}
catch( Exception ex )
{
Debug.WriteLine( ex.Message );
}
-
October 9th, 2008, 07:49 AM
#6
Re: How do I run an external process from C#?
"You really should be validating this line."
I've tried a hard coded path:
Code:
Process.Start("explorer.exe", "C:\\");
and the problem still occurs.
???
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|