///
/// 返回指示文件是否已被其它程序使用的布尔值
///
/// 文件的完全限定名,例如:“C:MyFile.txt”。
/// 如果文件已被其它程序使用,则为 true;否则为 false。
public Boolean GO_FileIsUsed(String fileFullName)
{
Boolean result = false;
//判断文件是否存在,如果不存在,直接返回 false
if (!System.IO.File.Exists(fileFullName))
{
result = false;
}
else
{
//看看文件是否能用程序打开。
System.IO.FileStream fileStream = null;
try
{
fileStream = System.IO.File.Open(fileFullName, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
//能正常打开
result = false;
}
catch (System.IO.IOException ioEx)
{
//出错说明不能打开,返回true,正在被占用
result = true;
}
catch (System.Exception ex)
{
result = true;
}
finally
{
if (fileStream != null)
{
fileStream.Close();
}
}
}
return result;
}
using System;
using System.Threading;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//让线程能够操作button的值,省着用委托
CheckForIllegalCrossThreadCalls = false;
//timer开始判断
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
//每隔一小段时间去检查一下这个文件是否被占用
Thread thread = new Thread(Go);
thread.Start();
}
private void button1_Click(object sender, EventArgs e)
{
}
void Go()
{
if(GO_FileIsUsed("D:1a.docx"))
{
button1.Text = "正在被占用";
}
else
{
button1.Text = "未被占用";
//没有被占用的话,就可以做其它事了...
}
}
public Boolean GO_FileIsUsed(String fileFullName)
{
Boolean result = false;
//判断文件是否存在,如果不存在,直接返回 false
if (!System.IO.File.Exists(fileFullName))
{
result = false;
}
else
{
//看看文件是否能用程序打开。
System.IO.FileStream fileStream = null;
try
{
fileStream = System.IO.File.Open(fileFullName, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
//能正常打开
result = false;
}
catch (System.IO.IOException ioEx)
{
//出错说明不能打开,返回true,正在被占用
result = true;
}
catch (System.Exception ex)
{
result = true;
}
finally
{
if (fileStream != null)
{
fileStream.Close();
}
}
}
return result;
}
}
}
using System;
using System.Threading;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool OK = false;
private void Form1_Load(object sender, EventArgs e)
{
//让线程能够操作button的值,省着用委托
CheckForIllegalCrossThreadCalls = false;
Thread thread = new Thread(Go);
thread.Start();
}
private void button1_Click(object sender, EventArgs e)
{
}
void Go()
{
while (true)
{
if (GO_FileIsUsed("D:1a.docx"))
{
button1.Text = "正在被占用";
}
else
{
OK = true;
button1.Text = "未被占用";
break;
}
}
}
public Boolean GO_FileIsUsed(String fileFullName)
{
Boolean result = false;
//判断文件是否存在,如果不存在,直接返回 false
if (!System.IO.File.Exists(fileFullName))
{
result = false;
}
else
{
//看看文件是否能用程序打开。
System.IO.FileStream fileStream = null;
try
{
fileStream = System.IO.File.Open(fileFullName, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
//能正常打开
result = false;
}
catch (System.IO.IOException ioEx)
{
//出错说明不能打开,返回true,正在被占用
result = true;
}
catch (System.Exception ex)
{
result = true;
}
finally
{
if (fileStream != null)
{
fileStream.Close();
}
}
}
return result;
}
}
}
页面更新:2024-04-28
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号