有人可以解释一个人如何处理文件共享

有人可以解释一个人如何处理文件共享

本文介绍了有人可以解释一个人如何处理文件共享。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI伙计们,



- 我有一个应用程序会将其数据记录到abc.txt文件中。



-此应用程序由15-20人共享,所有人都需要登录

abc.txt文件。



- 我有另一个应用程序,它将从abc.txt文件中读取数据,以生成关于此应用程序的一些

统计数据



我将给我看一些代码我如何处理这个filesharig的事情。



我的记录:

HI guys,

-So i have an application which will log its data into abc.txt file.

-This application is shared between 15-20 people which all of them need to log in
abc.txt file aswell.

- I have another application which will read data from abc.txt file to generate some
statistics about this application

I will show you some code how i am handling this filesharig thing.

in my logging :

<appender name="TestAppender" type="log4net.Appender.RollingFileAppender">
  <lockingmodel type="log4net.Appender.FileAppender+MinimalLock"/>
  <param name="File" value="c://temp//abc.txt" /> //temporary path for question.
  <param name="AppendToFile" value ="true"/>
  <encoding value="utf-8" />
  <rollingStyle value="Date" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %level [%thread] %type.%method   %message%n" />
  </layout>
</appender>







在我的应用程序中我从abc.txt读取数据以生成统计:






in my application where i am reading the data from abc.txt to generate statistics :

string[] inputFilePaths = Directory.GetFiles("\\Path to directory where abc.txt is");


           DateTime now = DateTime.Now;
           string[] thisMonth = inputFilePaths.Where(f => IsThisMonth(now, f)).ToArray();

           using (StreamWriter outputStream = new StreamWriter("//path to a file where i want to write a motnh statistics.. abc.txt is included",false))
           {
               foreach (var inputFilePath in inputFilePaths)
               {
                   using (var inputStream = new StreamReader(inputFilePath))
                   {
                      outputStream.Write(inputStream.ReadToEnd());
                   }

               }
           }







我用于上述方法的方法:




Method i am using for the above one :

private bool IsThisMonth(DateTime now, string path)
      {
          DateTime dt;
          if (!DateTime.TryParseExact(Path.GetExtension(path),
                                      ".yyyy-MM-dd",
                                      CultureInfo.InvariantCulture,
                                      DateTimeStyles.None,
                                      out dt))
          {
              return true;
          }
          return dt.Year == now.Year && dt.Month == now.Month;
      }







现在谈到这一行:




now when it comes to this line :

using (var inputStream = new StreamReader(inputFilePath))





将出现错误:S



an error will appear : S

System.IO.IOException: 'The process cannot access the file '@.txt' because it is being used by another process.'





我的尝试:



有人可以向我解释这些东西是如何工作的以及解决方案是什么。



我发现我需要包含fileshare但是我不知道怎么做。



感谢您帮助我,如果您有任何疑问,请不要犹豫,问我。我还是学生,需要学习这些东西,因为它们将来会很有用。



What I have tried:

Can someone explain to me how this things work and what is a solution for this.

I have found that i need to include fileshare but i do not have an idea how to do so.

Thanks for helping me out and if you have any query do not hesitate to ask me. Im still a student and need to learn these things because they can be useful in future.

推荐答案

foreach (var inputFilePath in inputFilePaths)
{
    var fs = File.Open(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    using (var inputStream = new StreamReader(fs))
    {
       outputStream.Write(inputStream.ReadToEnd());
    }

}





此代码将以共享模式打开文件然后我将它转换为streamreader:)



This code will open the file in share mode then i am converting it intom streamreader :)


这篇关于有人可以解释一个人如何处理文件共享。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:58