释放对文件的访问权限 [英] Releasing access to files

查看:149
本文介绍了释放对文件的访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有3个进程,它们按顺序写入特定文件,在某些情况下,当进程想要写入进程关闭的文件时(在写入的中间),因此访问另外2个进程的文件是麻烦,我添加了一个控制进程顺序的服务器进程,当进程关闭时会引发一个事件,那么当进程关闭时如何在此事件中释放源(文件访问)。像 File.Release(FilePath);

Assume we have 3 processes, they write to a specific file by order, in some cases when a process wants to write to the file the process closed (In middle of Writing), so access to file for another 2 processes is in trouble, I add a server process that control order of processes and when a process closed there is an event raised, so how can I release sources (file access) in this event when a process closed. Something like File.Release(FilePath);

编辑:

写入文件如下:

try {
        if(!File.Exists(FilePath))
            throw new Exception("File does not Exist.");

        bool Clear = false;
        using(StreamReader sr = new StreamReader(FilePath)) {
            if(sr.ReadToEnd().Length > 1200)
                Clear = true;
            }

        if(Clear)
            using(StreamWriter sw = new StreamWriter(FilePath, false)) {
                sw.WriteLine(Text);
                sw.Flush();
        } else
            using(StreamWriter sw = new StreamWriter(FilePath, true)) {
                sw.WriteLine(Text);
                sw.Flush();
        }
} catch(Exception ex) {}


推荐答案

using System.IO;

class Program
{
    static void Main()
    {
          using (StreamWriter writer = new StreamWriter("important.txt"))
          {
                writer.Write("Word ");
                writer.WriteLine("word 2");
                writer.WriteLine("Line");
          }
    }
}

这是我们的示例类型意思是说使用时,关键词使用。永远不要明确关闭streamwriter,也不要明确处理它。使用为你做了所有的魔力。它在工作完成后以及当你使用块时离开它时处理。

This is the example type we meant when saying use , keyword "using". Never close the streamwriter explicitly nor dispose it explicitly. using does all the magic for you. It disposes when the work is done and when you go out of this using block.

在多线程环境中,如果你明确地使用close或dispose,那么线程就会有考虑到线程可能只是尝试打开对象而前一个线程刚刚处理它的问题。同步线程时会遇到问题。这与服务器应用程序中通常面临的问题相同,后者具有多个连接到数据库的连接。这就是我们需要像Idisposable一样使用它的原因。

In a multi-threaded environment if you use close or dispose explicitly, the threads will have problem considering that the thread may just try to open the object while the previous thread just disposed it. you will have problems synchronizing your threads. This is the same problem that is normally faced in server applications which has multiple connections running into database. That's the reason we need to use it like this with Idisposable.

C#USING关键字 - 何时何时不使用?

这个更多地了解它。

更新:多个客户端同时写入单个文件是您的问题。正如上面的人所说,在写入模式下,没有两个进程可以同时保存同一个文件。如果您编写大数据,它必须等待,这个等待时间非常明显,或者您可以从客户端收集对象,对它们进行排队并从服务器实际写入它们。还可以避免将对服务器上的文件的写访问权限授予客户端。如果您的数据是序列化的,您可以使用谷歌协议缓冲区传递数据。

Update: multiple clients writing at the same time to a single file is your problem. As said by the people above, no two process can get hold of the same file at the same time in write mode. Either it has to wait if you write large data, this time of waiting is significantly noticeable or you can collect the objects from the clients, queue them and write them actually from the server. Giving write access on a file that is on the server to the client can also be avoided. If your data is serialized , the you could use google protocol buffers for passing the data.

如果您正在从客户端处理该文件,并且如果客户端进程在中途终止,则该文件现在没有写入句柄,显然它将是另一个进程可以通过写访问权限获取句柄。

If you are handling the file from the client and if the client process is terminated mid-way, there will be no write handle to the file now and obviously it will be free for another process to acquire a handle on it with write access.

这篇关于释放对文件的访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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