Re: How to close another program with C#
i get an error when i use "Process []pArry = Process.GetProcesses();"
how do i get this to work
thanks
Re: How to close another program with C#
Quote:
Originally Posted by session
I want the MesageBox to show when windows media player is not running but it just keeps loopin even when wmp is running
That's because you show the messagebox inside a loop. Do this in stead:
Code:
bool found = false;
foreach(Process p in pArry)
{
string s = p.ProcessName;
s = s.ToLower();
if (s.CompareTo("wmplayer") ==0)
{
found = true;
break;
}
else
{
//Do nothing
}
}
if(!found)
{
MessageBox.Show("Windows Media Player is not running","",System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
Application.Exit();
}
EDIT: Didn't see the second page :P
Re: How to close another program with C#
Quote:
Originally Posted by SilverFire
i get an error when i use "Process []pArry = Process.GetProcesses();"
how do i get this to work
thanks
What is the error?
Re: How to close another program with C#
Quote:
Originally Posted by session
What should I do to these codes?
I want the MesageBox to show when windows media player is not running but it just keeps loopin even when wmp is running
Try...
Code:
System.Diagnostics.Process[] _procs = System.Diagnostics.Process.GetProcessesByName("wmplayer");
if (_procs.Length != 0)
MessageBox.Show("Media Player is running");
else
MessageBox.Show("Media Player is not running");
Edit: Didn't see the second page either :blush:
Re: How to close another program with C#
the error is
An unhandled exception of type 'System.InvaildOperationException' occurred in system.dll
Additional information: Couldn't get process information from remote machine.
Re: How to close another program with C#
Quote:
Originally Posted by SilverFire
the error is
An unhandled exception of type 'System.InvaildOperationException' occurred in system.dll
Additional information: Couldn't get process information from remote machine.
At this point it's pretty hard to understand what code you are trying.
Can you zip up and post a small project (minus the debug folders) so we can take a look at it?
1 Attachment(s)
Re: How to close another program with C#
At this point it's pretty hard to understand what code you are trying.
Can you zip up and post a small project (minus the debug folders) so we can take a look at it?
here's the code
Re: How to close another program with C#
First of all, I suggest you upgrade from .Net 1.0. There have been significant upgrades since the 1.0 release (current is 3.5). You can also get express versions of Visual Studio 2005 or 2008 for free.
The problem with your 'Kill' code is that you are trying to compare the full path of an executable with it's process name.
For example, consider notepad:
Fullpath "C:\Windows\notepad.exe"
Process Name "notepad"
So the process killing code inside your sleep handler isn't going to find the notepad process.
Here's an alternative that works.
Code:
private void btnSleep_Click(object sender, System.EventArgs e)
{
string processName = ExtractProcessName( ofdBrowse1.FileName );
foreach( Process p in Process.GetProcesses( ) )
{
if( 0 == String.Compare( processName, p.ProcessName, true ) )
{
p.Kill();
}
}
}
private string ExtractProcessName( string fileName )
{
string processName = fileName;
int index = processName.LastIndexOf( "\\" );
if( -1 != index )
{
processName = processName.Substring( index + 1, processName.Length - index - 1 );
}
processName = processName.ToUpper( );
processName = processName.Replace( ".EXE", "" );
return processName;
}
Btw, Kill should only be used as a last resort. See the msdn docs for Process.Kill which recommends using CloseMainWindow to close processes.
Re: How to close another program with C#
private void btnSleep_Click(object sender, System.EventArgs e)
{
string processName = ExtractProcessName( ofdBrowse1.FileName );
foreach( Process p in Process.GetProcesses( ) )
{
if( 0 == String.Compare( processName, p.ProcessName, true ) )
{
p.Kill();
}
}
}
private string ExtractProcessName( string fileName )
{
string processName = fileName;
int index = processName.LastIndexOf( "\\" );
if( -1 != index )
{
processName = processName.Substring( index + 1, processName.Length - index - 1 );
}
processName = processName.ToUpper( );
processName = processName.Replace( ".EXE", "" );
return processName;
}
I have version 3.5 and i still get an error on
Process.GetProcesses( )
Re: How to close another program with C#
Quote:
Originally Posted by SilverFire
I have version 3.5 and i still get an error on
Process.GetProcesses( )
When you ask help from a programming forum, there are a few basics you need to know.
1) Describe the code
2) Describe the problem
3) Include any relevant code snippets
4) Include any specific errors
We can't help you if you don't tell us what the specific error message is.