C#判断文件是否被占用及其应用

我们在操作配置文件的时候,或者是导出一些文件的时候,这个文件是被占用状态,此时无法对文件做其它操作,那么我想等这个文件被其它操作的占用解除后,再对其进行操作,我该如何知道这个文件是否能被操作呢?效果图在文章最后。


1、首先我们需要对该文件是否被占用做一个判断,来看下面这个函数

/// 
/// 返回指示文件是否已被其它程序使用的布尔值
/// 
/// 文件的完全限定名,例如:“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;
}

2、我们在另一个线程中随时判断这个文件是否被占用,等到占用解除时再做其它操作

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;
        }
    }
}

真正的应用场景,可以在让这个窗体做为一个等待窗体showdialog(),然后文件解除占用后,关闭这个窗体,就可以继续往下操作了。

展开阅读全文

页面更新:2024-04-28

标签:文件   窗体   能用   线程   效果图   函数   指示   场景   操作   程序

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top