I'm trying to create/code a useful Tool in C# and could need a little bit of help, with a specific problem.
I'd like to know if there is any way to end ALL Processes which try to access on a file, so I can Open & edit the specific file.
The problem is that I'm trying "System.IO.File.WriteAllLines()" on a file which is already occupied by another process.
System.IO.StreamReader file = new System.IO.StreamReader(settingsPath);
try
{
while (file.ReadLine() != null)
{
String lineContent = file.ReadLine();
if (lineContent.Contains(whichSettingString) && lineContent != null)
{
lineContent = lineContent.Replace(whichSettingString, NewValue);
completeLineContent[0] = lineContent;
}
lineContent = lineContent + lineContent;
}
} catch (Exception)
{
}
//file.Dispose();
file.Close();
System.IO.File.WriteAllLines(settingsPath, completeLineContent);
My IDE Visual Studio is throwing an exception at the last line, saying that the file is already occupied by another process. The code per se is working I tried to save/write the Output of this block into another file and it's working. So the only Idea I got that may help me is a line of code or probably more than 1 line to "kill" all processes that are trying to access a file.
Already googl'ed a while and checked Stack Overflow for this problem, but didn't find a solution so far. This is my first comment on Stack Overflow and I'm kinda new to coding, so if any additional information is needed pls feel free to ask :-)
Thank you very much best regards Nico