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" />
<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.