如何检查文件当前是打开还是在.NET中写入? [英] How to check if a file is currently open or being written to in .NET?

查看:162
本文介绍了如何检查文件当前是打开还是在.NET中写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定文件当前是打开还是使用C#写入。我已经看到类似的SO问题,所有问题都与我的代码类似,它在文件上尝试File.Open。但是当我使用下面的代码运行程序时,我也手动打开文件,我得到文件当前未锁定的意外结果。任何想法/建议/我遗失的任何东西?

I am trying to determine whether or not a file is currently open or being written to using C#. I've seen similar SO questions, all with a similar solution as my code, which attempts a File.Open on a file. But when I run the program with the code below, and I also manually have the file open, I get the unexpected result of "File is currently NOT locked". Any thoughts/suggestions/anything I'm missing?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace TestIfFileAccessed
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\TEMP\testFile.txt";
            FileInfo filepath = new FileInfo(path);

            if (IsFileLocked(filepath)) {
                Console.WriteLine("File is currently locked");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("File is currently NOT locked");
                Console.ReadLine();
            }
        }

        public static bool IsFileLocked(FileInfo file)
        {
            FileStream stream = null;

            try
            {
                stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            }
            catch (IOException)
            {
                //the file is unavailable because it is:
                //still being written to
                //or being processed by another thread
                //or does not exist (has already been processed)
                return true;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }

            //file is not locked
            return false;
        }
    }
}


推荐答案

文本文件通常不会被锁定 - 它们的信息通常被拉入并加载到内存中,所以除非你试图在另一个程序正在加载文件的同一时刻访问它(这会非常快)然后你应该没有任何问题。

Text files aren't usually locked - their info is usually pulled and loaded into memory, so unless you were trying to access it at the exact same moment that another program is loading the file (which would go very quickly) then you shouldn't have any problems.

来源 - 类似问题

编辑:如果它是单词打开那么你会遇到问题,因为Word保留了流开。尝试在Word中打开文件(如果有的话)并再次运行代码,我相信它应该可行。否则,如果要查看它是否在记事本中打开,则必须扫描系统上运行的当前进程以获取记事本,并检查进程是否打开了该文件。

If it is open in word then you will have problems, as Word keeps the stream open. Try opening the file in Word (if you have it) and running your code again, I believe it should work. Otherwise, if you want to see if it's open in Notepad, you would have to scan the current processes running on the system for notepad and check if the process opened the file.

这篇关于如何检查文件当前是打开还是在.NET中写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆