0

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

  • 1
    change the following into a `using` statement: `System.IO.StreamReader file = new System.IO.StreamReader(settingsPath);` – jazb Oct 23 '18 at 07:39
  • 3
    You would have to kill your own application since you are the one having the lock. The `Close` can have an effect a little longer than the execution of `Close`. A `using` statement would fix some of it. – Patrick Hofman Oct 23 '18 at 07:40
  • disagree - just the using statement will fix your problem. – jazb Oct 23 '18 at 07:48
  • Instead of killing the processes what about just releasing the locking handles? This is what similar tools (Unlocker, LockHunter, etc.) also do. Btw in this case you lock that file so a simple `using` block would solve the problem. – György Kőszeg Oct 23 '18 at 07:49
  • 1
    @PatrickHofman "The Close can have an effect a little longer than the execution of Close" - that's not true - when `Close()` returns, it is guaranteed that the underlying file handle will have been released and the file will be fully closed. If this wasn't the case, a lot of code that closes and then immediately reopens a file would fail. (Unless I misunderstood what you meant!) – Matthew Watson Oct 23 '18 at 07:50
  • as a side note - you do realize this code will end up skipping over the first line right? – jazb Oct 23 '18 at 07:54
  • What is the purpose of this line: `completeLineContent[0] = lineContent;`? You're always setting the first element of the `completeLineContent` array but you're not doing anything with it. – Rui Jarimba Oct 23 '18 at 07:54
  • The reason you didn't find an answer is that there is no quick answer for this, OP. You have to *troubleshoot* the issue and find out where those locks are coming from. You can't just go around killing processes like that. – John Wu Oct 23 '18 at 07:56
  • OP Here, thanks for those quick answers :-)!! I think the using statement isn't helping in my case I tried to add it into my code and still got the same error. I probably open the file somewhere in my program and forgot to close it like John Wu mentioned, the problem is I can't find the place where it isn't closed. RuiJarimba yeah this line is useless as you said, I implemented it coz I was working with the array, earlier unless I realized I could do it without this line :-) JohnB tbh I didn't know that the first line would be skipped, I'm very new to coding at all :P – NicoTriesToCode Oct 23 '18 at 07:57
  • there is a quick answer - the `using` statement – jazb Oct 23 '18 at 07:57
  • Also, if you need to read all lines of a text file, you could use `File.ReadLines` or `File.ReadAllLines` instead - see https://stackoverflow.com/a/8038746/558486 – Rui Jarimba Oct 23 '18 at 07:58
  • This has been covered before, I know cos I used the code myself : https://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net – BugFinder Oct 23 '18 at 07:59
  • Possible duplicate of [How do I find out which process is locking a file using .NET?](https://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net) – BugFinder Oct 23 '18 at 07:59
  • 1
    Okay, after putting a using() around any statement wherever I use the file, it worked. Thank you all so much! This helped a lot! – NicoTriesToCode Oct 23 '18 at 08:08
  • Even if it does turn out to be other processes accessing the file (and not your own), it's rather tricky to do what you've asked for (avoiding race conditions) without massive privileges. You have to suspend every thread in every other process to prevent any of them *opening* a new handle in the gap between "kill all other processes" and "now I'm going to open the file myself" – Damien_The_Unbeliever Oct 23 '18 at 08:08

0 Answers0