Reputation: 21
I've created a service worker which reads in serial port information and creates a .csv file to save the results to.
However, when i attempt to write to the file it says that the process cannot access the file, despite (from my knowledge) the file not being open, used by another external process or the filestream being open (i'm using System.IO.File.WriteAllLines()). What could the problem be?
static string InsturmentFile = $"{Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)}\\TestLogger\\";
public static void StartConnector()
{
Directory.CreateDirectory(InsturmentFile);
}
public static string FullFilePath(this string fileName)
{
return $"{InsturmentFile}\\{fileName}";
}
public static List<string> LoadFile(this string file)
{
if (!System.IO.File.Exists(file))
{
return new List<string>();
}
return System.IO.File.ReadAllLines(file).ToList();
}
public static List<InstrumentRawReading> ConvertToInstrumentRawReadingModel(this List<string> lines)
{
List<InstrumentRawReading> output = new List<InstrumentRawReading>();
foreach (string line in lines)
{
string[] cols = line.Split(',');
InstrumentRawReading u = new InstrumentRawReading();
string spacer = " ";
var now = DateTime.Now;
now = DateTime.Parse(cols[0]);
foreach (var reading in u.Readings)
{
reading.Name = cols[1];
}
output.Add(u);
}
return output;
}
public static void SaveReadingsLogToFile(this List<InstrumentRawReading> models, string fileName)
{
try
{
List<string> lines = new List<string>();
foreach (InstrumentRawReading u in models)
{
foreach (var rd in u.Readings)
{
lines.Add($"{rd.Time},{u.SerNo},{rd.Name},{rd.Rdg},{rd.Units}");
}
}
System.IO.File.WriteAllLines(fileName.FullFilePath(), lines);
}
catch (Exception ex)
{
Console.WriteLine("SaveReadingsLogToFile: " + ex.Message.ToString());
throw;
}
}
This is the full file creation class which i'm using the create the .csv file, it is being created and populated with one reading, however it seems that on the subsequent calls of the method it just stops working.
Upvotes: 0
Views: 9954
Reputation: 21
So one of the issues with my approach was that the serial port information was being read in so quickly and in multiple places that the calls became almost simultaneous. A quick fix(hack) was the add a slight delay before or after the call which seemed to do the job, however an easier fix would be to limit the number of calls made to the function from the serial port, as this would ensure that the file is always open for use, as the SaveReadingLogToFile() isn't being called multiple times from different threads/places. The best fix for this i feel, would be to write the operations to a queue and/or add a lock around the writing of the file.
Upvotes: 1
Reputation: 4440
If the error is Process cannot access the file because it is being used by another process
then it IS used by another process no matter what you say.
You can use Process Explorer a nice tool from Microsoft that you can search literal for your file and it will tell you which process has the lock on your file.
Click Find
/ Find Handle or DLL..
and just type in your file name
Upvotes: 1