Click here to Skip to main content
15,885,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>



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))


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


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.
Posted
Updated 13-Aug-18 20:34pm
Comments
TommoDotCommo 14-Aug-18 2:12am    
Have you checked to see whether you can read/write/modify the file using a text editor or the Windows Explorer? It may be a permissions issue.
Joe Doe234 14-Aug-18 2:16am    
when i used filestream i did FIleShare.readwrite, but now i had to change things to streamwriter/reader and this issue pops.. maybe there a way i can do the filesharing thing to streamwriter/reader.

1 solution

I just found the solution :D


I have added the following code :
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());
    }

}


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900