Note that there are some explanatory texts on larger screens.

plurals
  1. POSome trouble with locking files for a longer time
    primarykey
    data
    text
    <p>I have a function that is reading a file and adding some of the string in a list and returning this list. Because I wanted that nobody and nothing could change, delete or whatever the current file that I was reading I locked it. Everything was fine, I did it somehow like this:</p> <pre><code>public static List&lt;string&gt; Read(string myfile) { using (FileStream fs = File.Open(myfile, FileMode.Open, FileAccess.Read, FileShare.None)) { //read lines, add string to a list //return list } } </code></pre> <p>Thats fine. Now I have another function in another class that is doing stuff with the list and calling other functions and so on. Now sometimes I want to move the file that I was reading. And here is the problem: because Im now in a new function and the function <code>Read(string myfile)</code> is already processed, there is no more lock in the file.</p> <pre><code>//in another class public static void DoStuff(/*somefile*/) { List&lt;string&gt; = Read(/*somefile*/); //the file (somefile) is not more locked! //do stuff if (something) Move(/*somefile*/) //could get an error, file maybe not more there or changed... } </code></pre> <p>So another function/user could change the file, rename it, deleting it or whatever => Im not able to move this file. Or I will move the changed file, but I dont what that. If I would use threading, another thread with the same function could lock the file again and I could not move it.</p> <p>Thats why I somehow need to lock this file for a longer time. Is there an easy way? Or do I have to replace my <code>using (FileStream fs = File.Open(myfile, FileMode.Open, FileAccess.Read, FileShare.None)</code> code? Any suggestions? thank you</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload