int lock(const char * filepath )
{
int fd = 0
struct flock flock
memset( &flock, 0, sizeof(flock) )
fd = open( filepath, O_RDWR,S_IRUSR|S_IWUSR )
if ( fd <0 )
{
printf("open error!\n")
return ( -1 )
}
flock.l_type = F_WRLCK
flock.l_whence = SEEK_SET
flock.l_start = 100000
flock.l_len = 0
if ( fcntl( fd, F_SETLK, &flock ) ) //加锁,如果失败则等待10秒
{
printf("cannot set lock\n")
printf("the process will retry after 10 seconds\n")
sleep(10) //不同版本的sleep单位可能不一样,有的是秒有的是毫秒,你自己看着改
if ( fcntl( fd, F_SETLK, &flock ) ) //加锁,如果失败则退出
return( -1 )
}
return( 0 )
}
后面的你懂吧
string fileName = @"c:\aaa.doc"//要检查被那个进程占用的文件Process tool = new Process()
tool.StartInfo.FileName = "handle.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)).Kill()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)