Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:38:06+00:00 2026-06-11T11:38:06+00:00

I have a function that is reading a file and adding some of the

  • 0

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:

public static List<string> Read(string myfile)
{
  using (FileStream fs = File.Open(myfile, FileMode.Open, FileAccess.Read, FileShare.None))
  {
    //read lines, add string to a list
    //return list
  }
}

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 Read(string myfile) is already processed, there is no more lock in the file.

//in another class
public static void DoStuff(/*somefile*/)
{
  List<string> = 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...
}

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.

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 using (FileStream fs = File.Open(myfile, FileMode.Open, FileAccess.Read, FileShare.None) code? Any suggestions? thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:38:08+00:00Added an answer on June 11, 2026 at 11:38 am

    If you want to keep the file locked for longer then you need to refactor your code so that the Stream object is kept around for longer – I would change the Read method to accept a FileStream, a little bit like this

    using (FileStream fs = File.Open(myfile, FileMode.Open, FileAccess., FileShare.None))
    {
        List<string> = Read(fs);
        if (something)
        {
            File.Move(/* somefile */)
        }
    }
    

    The problem you are going to have is that File.Move method is going to fail as this file is already locked (by you, but File.Move doesn’t know that).

    Depending on what exactly it is you want to do it might be possible to work out a way of keeping the file locked while also “moving” the file, (for example if you know something in advance you could open the file specifying FileOptions.DeleteOnClose and write a new file with the same contents in the desired destination), however this isn’t really the same as moving the file and so it all depends on what exactly it is you are trying to do.

    In general such things are almost always going to be more trouble than they are worth – you are better off just unlocking the file just before you move it and catching/ handling any exception that is thrown as a result of the move.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading about the function of __init__.py file. It is said that
I have the following code inside a function that is reading in a file
In my Java code I have function that gets file from the client in
I have a function that returns two values in a list. Both values need
I have a function that takes an input string and then runs the string
Ok, so I have a function that takes the path to an image file
I have a function that reads integers with certain format from a file. It
In my code, I have a load_dataset function that reads a text file and
I have a function that I'm using to download a file on the server
I have this function that iterates through a directory and is supposed to read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.