How to close another application/program with c#, like eg, how to close Word with c# or how to RUN photoshop with c#
//it's not too complicated i think
Printable View
How to close another application/program with c#, like eg, how to close Word with c# or how to RUN photoshop with c#
//it's not too complicated i think
Process []pArry = Process.GetProcesses();
foreach(Process p in pArry)
{
string s = p.ProcessName;
s = s.ToLower();
if (s.CompareTo("winword") ==0)
{
p.Kill();
}
}
if u want to run photoshop with C# then
use
Process.Start("...");
where .. is the path of the exe.
-Paresh
Quote:
Originally posted by pareshgh
Process []pArry = Process.GetProcesses();
foreach(Process p in pArry)
{
string s = p.ProcessName;
s = s.ToLower();
if (s.CompareTo("winword") ==0)
{
p.Kill();
}
}
what if it's not word but other program???
well you can write what the program name is or pass from command line parameters or pass by reference to some objects.
-Paresh
can i use its path?
well yes actually, but you will need to cut it in order to get its process , as In my above sample "winword" is the process you are looking for.
Paresh
hmmmm how do i use path then? just put path in the winword place? and about the order, i don't quiet get it :S
what r u trying to do ?
-Paresh
first off, close a program with c#. but i wish i could put something like "C:\\program files\\microsoft office\\office 11\\winword.exe" instead of "winword"
second, shut down windows with c#?
and thanks so much for help! :)
hi,
you can close program with the same above code.Quote:
first off, close a program with c#.
check the Process class in more details. if FullName offers that and if it tells where it was loaded from then I think you will be able to do that.Quote:
but i wish i could put something like "C:\\program files\\microsoft office\\office 11\\winword.exe" instead of "winword"
Hmm That's nice. C# will close your application and windows then what will you work ! :D :D LOLQuote:
second, shut down windows with c#?
ok I will find and tell you. may be someone already knows can tell.
-Paresh
lol naw, i wanna write a program that will shut down my computer after say 60 mins so my computer will shut down after i go to sleep! :p
What should I do to these codes?Code:foreach(Process p in pArry)
{
string s = p.ProcessName;
s = s.ToLower();
if (s.CompareTo("wmplayer") ==0)
{
//Do nothing
}
else
{
MessageBox.Show("Windows Media Player is not running","",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
Application.Exit();
break;
}
}
I want the MesageBox to show when windows media player is not running but it just keeps loopin even when wmp is running
and how can i control the volume in wmp with c#? someone said use directx but im not that good. can somebody explain how to do it?
thanks a lot! :)
anyone? :confused:
If you are warking with winforms...
you can use the win32 api.
A week ago pareshgh showed me that the functions of the win32 api are availabe if you call the
using System.Runtime.InteropServices
you can
[DllImport("user32.dll")]
and from there I know from coding (in vb) that you can shut the computer).
If no one will give you a better solution I'll look the api code for you.
thans for help n please look up the code if thats convenient for u! :) i've searched in msdn for a while already but i couldnt find sumthin useful :S
I'm sorry that I don't have the time right now
but a quick search in google
gave me
http://www.mvps.org/vbnet/index.html...twindowsex.htm
first url
which looks real good
and this one is of microsoft (hope they know something about their OS ;)
second url
http://support.microsoft.com/default...NoWebContent=1
if you are trying to run the audio file.
herez the sample for you.
(the sample for playing the audio files
without any GUI)
u might consider running it in threads if u need
check the sample.
using
winmm.dll
[DllImport("winmm.dll")]
public static extern long PlaySound(String lpszName, long hModule, long dwFlags);
-----------------check the following code------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.IO;
namespace WinMediaPlayer
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
[DllImport("winmm.dll")]
public static extern long PlaySound(String lpszName, long hModule, long dwFlags);
private System.Windows.Forms.Button buttonPlay;
private System.Windows.Forms.Label labelFile;
private System.Windows.Forms.TextBox textBoxAudioFile;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonPlay = new System.Windows.Forms.Button();
this.labelFile = new System.Windows.Forms.Label();
this.textBoxAudioFile = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// buttonPlay
//
this.buttonPlay.Location = new System.Drawing.Point(128, 80);
this.buttonPlay.Name = "buttonPlay";
this.buttonPlay.Size = new System.Drawing.Size(96, 32);
this.buttonPlay.TabIndex = 0;
this.buttonPlay.Text = "Play";
this.buttonPlay.Click += new System.EventHandler(this.buttonPlay_Click);
//
// labelFile
//
this.labelFile.Location = new System.Drawing.Point(8, 24);
this.labelFile.Name = "labelFile";
this.labelFile.Size = new System.Drawing.Size(80, 24);
this.labelFile.TabIndex = 1;
this.labelFile.Text = "File To Play";
//
// textBoxAudioFile
//
this.textBoxAudioFile.Location = new System.Drawing.Point(112, 24);
this.textBoxAudioFile.Name = "textBoxAudioFile";
this.textBoxAudioFile.Size = new System.Drawing.Size(328, 20);
this.textBoxAudioFile.TabIndex = 2;
this.textBoxAudioFile.Text = "Path to Audio File";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(424, 141);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBoxAudioFile,
this.labelFile,
this.buttonPlay});
this.Name = "Form1";
this.Text = "Windows Media Player ";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void buttonPlay_Click(object sender, System.EventArgs e)
{
// Codes used
// where Asynchronously = 0x0001,
// where NoWait = 0x00002000,
// where Filename = 0x00020000,
if(File.Exists(textBoxAudioFile.Text))
{
PlaySound(textBoxAudioFile.Text, 0,0x0001|0x00020000|0x00002000);
}
}
}
}
----------------------------------------------------
hope this helps you
-Paresh
it works great! I just tried it and I still have to read the code over but appreciate for your help! :)
i get an error when i use "Process []pArry = Process.GetProcesses();"
how do i get this to work
thanks
That's because you show the messagebox inside a loop. Do this in stead:Quote:
Originally Posted by session
EDIT: Didn't see the second page :PCode: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();
}
What is the error?Quote:
Originally Posted by SilverFire
Try...Quote:
Originally Posted by session
Edit: Didn't see the second page either :blush: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");
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.Quote:
Originally Posted by SilverFire
Can you zip up and post a small project (minus the debug folders) so we can take a look at it?
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
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.
Btw, Kill should only be used as a last resort. See the msdn docs for Process.Kill which recommends using CloseMainWindow to close processes.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;
}
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( )
When you ask help from a programming forum, there are a few basics you need to know.Quote:
Originally Posted by SilverFire
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.
I really want to Add Silverfire Please:Quote:
Originally Posted by Arjay
USE CODE TAGS
I am moved to know of your attempts, I have got to leave for home to climb the coconut tree for coconuts now.
Yes a code tag looks like the color tags I have added in to indicate the purple. [Purple makes me really sad], I don't give purple yam soup any more ingredients. Promise you Ali
?? :lol: Regarding CodeTags They are not Colortags Look at the bottom of my post or read the forum rules and you will see how they work.Quote:
Originally Posted by Pimpya
Don't fall out of the tree.Quote:
Originally Posted by Pimpya
just wondering if anybody else noticed that the original post was from over 5 years ago?
Yes, but when people are falling out of trees, does it really matter?Quote:
Originally Posted by eclipsed4utoo
Therefore I adressed to Silverfire who obviously took the old thread to revived the old thread for his own question. He's the new requester obviously in my eyes. Pimpis falling out of the tree because of looking for coconuts :D are only funnysideeffects :lol: :lol: :lol:Quote:
Originally Posted by eclipsed4utoo
For this, You dont even need a C# program. You may do a task scheduler with a command as follows:Quote:
Originally Posted by session
Steps:
[Win] | [Control Panel] | [Scheduled Tasks] | [File\New\Scheduled Task]
Under Run, key in this
C:\WINDOWS\system32\shutdown.exe -s -f
Under Start in, key in this
C:\WINDOWS\system32
You can then set the scheduled task to run at the time you want it to run. Voila! Your system will shutdown when you are having a sweet dream.