gpt4 book ai didi

c# - 确定可执行文件是否正在运行

转载 作者:太空宇宙 更新时间:2023-11-03 18:03:21 25 4
gpt4 key购买 nike

我不知道我在这里做错了什么,但我试图在它没有运行时删除一个二进制文件,但如果它正在运行,则显示一个消息框告诉用户在删除它之前关闭程序.当我尝试这样做时,它会忽略消息框并在运行时尝试删除文件,显然你不能这样做,所以 Visual Studio 所以它返回:

System.UnauthorizedAccessException: 'Access to the path 'C:\cmctemp\lcpol\lcweb.exe' is denied.'

不确定为什么不显示消息框。

private void button5_Click(object sender, EventArgs e)
{
Process[] pname = Process.GetProcessesByName("lcweb.exe");
if (pname.Length == 0)
if (File.Exists(@"C:\cmctemp\lcpol\lcweb.exe"))
File.Delete(@"C:\cmctemp\lcpol\lcweb.exe");
else
MessageBox.Show("Please close the program before deleting!", "Information");
}

最佳答案

尝试这样做:

  private void button5_Click(object sender, EventArgs e)
{
Process[] pname = Process.GetProcessesByName("lcweb");
if (pname.Length == 0 && File.Exists(@"C:\cmctemp\lcpol\lcweb.exe"))
File.Delete(@"C:\cmctemp\lcpol\lcweb.exe");
else
MessageBox.Show("Please close the program before deleting!", "Information");
}

我们从 GetProcessByName 中删除了“.exe”的第一个更改,第二个更改我只是调整了您的 if 语句

希望对您有所帮助。

关于c# - 确定可执行文件是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42577287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com