c语言kill杀死excel进程,杀死由特定应用程序启动的excel进程

我会建议在(Excel)Process上做一些包装,当你完成它时你可以调用Close。

public class ExcelProcess

{

Process m_Process;

public ExcelProcess(/* parameters of your choice */)

{

// instantiate process and assign it to the m_Process variable

}

public ExcelProcess(int id)

{

// this will instantiate process object and hook to the process with specified process id

m_Process = Process.GetProcessById(id);

}

public void Close()

{

m_Process.Kill();

}

public override ToString()

{

// this will return process id as a string value

return m_Process.Id.ToString();

}

}

像你上面的一个有对象可以:

1.移动“实例”从项目到各地通过序列化的这些列表/阵列的一些文本文件(如.ToString())

2.有直接项目控制过程(每个ExcelProcess都可以有它自己的IPC与外部进程通信,可以重定向流和处理流),这将由该对象处理

3. 弄糊涂当其他人关闭应用程序时当前链接到该对象。

扩展以上几点,假设用户不能关闭与ExcelProcess相关的应用程序。

您可以创建一个持有者对象/字段,然后就可以serialzed:

List m_ExcelProcesses;

任何过程添加到它。

foreach(var process in Process.GetProcessesByName("excel"))

{

m_ExcelProcesses.Add(new ExcelProcess(process.Id));

}

现在让我们假设你想关闭他们在另一个应用程序:

byte[] nl = new byte[2] { 0x0D, 0x0A }; // new line

using (FileStream fs = File.Create("C:\SomeArbitrary\PathToFile.txt"))

{

foreach(ExcelProcess proc in m_Processes)

{

byte[] b = BitConverter.GetBytes(int.Parse(proc.ToString()));

fs.Write(b, 0, b.Length);

fs.Write(nl, 0, nl.Length);

}

}

之后,在新的过程,是负责关闭那些刚读所有的值和重写文件(或删除它)。

byte[] pidBytes = new byte[4]; // we stored int which is 4 bytes wide;

using (FileStream fs = File.OpenRead("C:\SomeArbitrary\PathToFile.txt"))

{

while(fs.CanRead)

{

fs.Read(pidBytes, 0, sizeof(int));

m_Processes.Add(new ExcelProcess(BitConverter.ToInt32(pidBytes, 0)));

fs.ReadByte(); // CR - 0x0D

fs.ReadByte(); // LF - 0x0A

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值