C# 强制关闭被占用的文件 根据文件路径,强制关闭所有占用文件的进程 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Process[] pcs = Process.GetProcesses(); foreach (Process p in pcs) { if (p.MainModule.FileName == "你的文件路径") { p.Kill(); } } 百度一下基本就又得 1、引发了“System.ComponentModel.Win32Exception”类型的异常2、没有根据路径找到对应的进程 可能会因为太深入底层 拒绝访问之类的你可以类似这样去实现 Process[] pcs = Process.GetProcesses(); foreach (Process p in pcs) { //确定文件进程名 if (p.ProcessName == "notepad") { //确认文件 if (p.MainWindowTitle.Contains("新建文本文档.txt")) { p.Kill();//结束进程 } } } 具体你去Debug处理好了 public static void KillFile(string fileName) { if (IsFileInUse(fileName)) { Process tool = new Process(); tool.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\handle64.exe"; tool.StartInfo.Arguments = fileName + " /accepteula"; tool.StartInfo.UseShellExecute = false; tool.StartInfo.RedirectStandardOutput = true; tool.Start(); tool.WaitForExit(); string outputTool = tool.StandardOutput.ReadToEnd(); string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)"; foreach (Match match in Regex.Matches(outputTool, matchPattern)) { Process.GetProcessById(int.Parse(match.Value)).InitializeLifetimeService(); } } } 1、引发了“System.ComponentModel.Win32Exception”类型的异常2、没有根据路径找到对应的进程他说的是关占用文件的进程,不见得是关文件本身,我有一个文件A,正在用迅雷下载,那么这个文件就被迅雷占用着,楼主的提问是杀掉迅雷。 这不就是wholockme嘛 求助: 将页面内容导出到文件,提示控件未放到RUNAT=“SERVER”标记内 为什么我的gridview.DataKeys.count总是为零?并提示索引超出范围 .net中为什么F12会失效 asp.net加载dll的问题 怎样判读当前日期是这个月的第几个星期的星期几 System.Text.Encoding.Default.GetByteCount 汉字+字符混合字符串宽度问题 这种功能用什么控件好 如何在DateGrid中显示图片 花了好几百分了都没解决,大哥们帮帮忙吧。 对于jsp技术,aspx技术的困惑 jqgrid 汇总列合计(分页了,不是本页合计,是总计)
foreach (Process p in pcs)
{
if (p.MainModule.FileName == "你的文件路径")
{
p.Kill();
}
} 百度一下基本就又得
1、引发了“System.ComponentModel.Win32Exception”类型的异常
2、没有根据路径找到对应的进程
你可以类似这样去实现 Process[] pcs = Process.GetProcesses();
foreach (Process p in pcs)
{
//确定文件进程名
if (p.ProcessName == "notepad")
{
//确认文件
if (p.MainWindowTitle.Contains("新建文本文档.txt"))
{
p.Kill();//结束进程
} }
}
public static void KillFile(string fileName)
{
if (IsFileInUse(fileName))
{
Process tool = new Process();
tool.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\handle64.exe";
tool.StartInfo.Arguments = fileName + " /accepteula";
tool.StartInfo.UseShellExecute = false;
tool.StartInfo.RedirectStandardOutput = true;
tool.Start();
tool.WaitForExit();
string outputTool = tool.StandardOutput.ReadToEnd(); string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";
foreach (Match match in Regex.Matches(outputTool, matchPattern))
{
Process.GetProcessById(int.Parse(match.Value)).InitializeLifetimeService();
}
}
}
1、引发了“System.ComponentModel.Win32Exception”类型的异常
2、没有根据路径找到对应的进程他说的是关占用文件的进程,不见得是关文件本身,
我有一个文件A,正在用迅雷下载,那么这个文件就被迅雷占用着,楼主的提问是杀掉迅雷。