string prfun(){
bool i=false;
process pName=Process.Start("ex.exe");
foreach (Process otherProc in
Process.GetProcessByName(pName.ProcessName))
{
if (pName.ProcessName== .ProcessName)
i=true;
if(i)
otherProc.Kill();
}
}
bool i=false;
process pName=Process.Start("ex.exe");
foreach (Process otherProc in
Process.GetProcessByName(pName.ProcessName))
{
if (pName.ProcessName== .ProcessName)
i=true;
if(i)
otherProc.Kill();
}
}
解决方案 »
- 求大神指点!!
- 请问能些出下面的代码代表能代表已经会了多少点技术了呢?
- 如何让窗体中的工具栏组件(下拉列表框)自动调整大小,类似于IE的地址栏文本框
- 随机生成密码时,随机数竟然重复... 在线求解...
- 我是个初学者,请问下“The name 'FileWatch' does not exist in the current context”是什么意思,
- 如何实现右击工具栏按钮
- +++++++++ 关于DllImport的问题 ++++++++++
- C#中的 "classpath"?[菜]
- 求助!!!C#中如何修改、删除、插入数据库里的数据
- 存储过程参数问题!
- C# 中如何获取系统目录?
- 我该怎么办??
bool i=false;
process pName=Process.Start("ex.exe");
foreach (Process otherProc in
Process.GetProcessByName(pName.ProcessName))
{
if (pName.ProcessName==otherProc .ProcessName)
i=true;
if(i)
otherProc.Kill();
}
}
bool i=false;
process pName=Process.Start("ex.exe");
foreach (Process otherProc in
Process.GetProcessByName(pName.ProcessName))
{
if (pName.ProcessName==otherProc .ProcessName)
i=true;
if(i)
otherProc.Kill();
}
}
szClassName, /*in*/LPCTSTR szWindowTitle)方法.
比较有用的是这句,我们可以使用registered window class name来找到所要窗口.
如: IE窗口(IEFrame是所有打开的IE的标识).protected void FindPopup()
{
IntPtr hParent = IntPtr.Zero;
IntPtr hNext = IntPtr.Zero;
String sClassNameFilter = "IEFrame"; // 所有IE窗口的
类
do
{
hNext =
NativeWIN32.FindWindowEx(hParent,hNext,sClassNameFilter,IntPtr.Zero);
// we've got a hwnd to play with if ( !hNext.Equals(IntPtr.Zero) )
{ // get window caption NativeWIN32.STRINGBUFFER
sLimitedLengthWindowTitle; NativeWIN32.GetWindowText(hNext, out
sLimitedLengthWindowTitle, 256); String sWindowTitle =
sLimitedLengthWindowTitle.szText; if (sWindowTitle.Length>0)
{ // find this caption in the list of banned captions
foreach (ListViewItem item in listView1.Items)
{ if ( sWindowTitle.StartsWith(item.Text) )
NativeWIN32.SendMessage(hNext, NativeWIN32.WM_SYSCOMMAND,
NativeWIN32.SC_CLOSE,
IntPtr.Zero); // try soft kill } } } }
while (!hNext.Equals(IntPtr.Zero)); } public class NativeWIN32{
[DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern
IntPtr FindWindowEx(IntPtr parent /*HWND*/,
IntPtr next /*HWND*/, string
sClassName, IntPtr
sWindowTitle); }
http://www.csdn.net/develop/read_article.asp?id=15534
http://www.csdn.net/develop/read_article.asp?id=15535
必须感谢TheAres 曾给我的指导。
引用
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection; 其他代码省略。仅仅从一个form启动讲。每个form都有个main入口。
static void Main()
{ Process instance = RunningInstance(); //获取运行中的实例
if (instance==null)
Application.Run(new Form1());
else
{
HandleRunningInstance(instance);//处理得到的进程。
}
}
private static Process RunningInstance()
{Process current = Process.GetCurrentProcess(); //得到当前form1的进程
Process[] processes = Process.GetProcessesByName(current.ProcessName);// 得到所有同名进程。
foreach(Process MyProcess in processes)
{
if(MyProcess.Id!= current.Id) //不同的进程id,也就是肯定不是同一进程
{
if(Assembly.GetExecutingAssembly().Location.Replace("/","\\")==current.MainModule.FileName) //进程名不同而id不同 的时候,比较程序启动的路径
return MyProcess;
}
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show("程序已经运行!");
ShowWindowAsync(instance.MainWindowHandle,1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);