[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 150
  • Last Modified:

File.WriteAllLines problem at random C# ASP.NET

Hello we have used this script to replace specific lane in a file:

static void lineChanger(string newText, string fileName, int line_to_edit)
{
     string[] arrLine = File.ReadAllLines(fileName);
     arrLine[line_to_edit - 1] = newText;
     File.WriteAllLines(fileName, arrLine);
}

Open in new window


We use similar like this :

lineChanger("education line" , "filetoreplace.txt" , 48);

Where 48 is the line to be replaced on the file. This is just an example.

Now the problem is at a random times this error : IOException: The process cannot access the file 'file path' because it is being used by another process

How can be modified the script to avoid that is not always is not always the error. The files we are replacing lines are .html files that run in the IIs web server.

I hope someone can help.

Thank you
0
Alex E.
Asked:
Alex E.
  • 3
  • 2
1 Solution
 
anarki_jimbelCommented:
First, you'd need to catch the error. If file is used  - you cannot change it.
Second - decide what to do. E.g.:
 - show an error message and advise user to repeat the operation
 - one more way is to make a number of attempts, say, 10, and if not successful - message to a user (see above) .

What behavior you want?
0
 
Ryan ChongCommented:
as mentioned in documentation:
Opens a text file, reads all lines of the file, and then closes the file.

ref:
File.ReadAllLines Method (String)
https://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx

File.ReadAllLines will close the file once it's finished its process, hence it will not be locked in the process.

>>Now the problem is at a random times this error : IOException: The process cannot access the file 'file path' because it is being used by another process
is this .html file being read by other processes/scripts in your system?
0
 
Alex E.Author Commented:
could you put an example of code on how to catch for that code in case if there is an error and retry the attempts you mention? I think is better that that see for the user a yellow screen of asp.net
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Alex E.Author Commented:
Well this .html files are courses online and the goal to replace specific lines is because we use forms in asp.net where the user block and unblock features but the html courses files are online and when the user apply the asp.net form immediately he or she see the change and user is able to block and unblock that features the the only thing that could call the html files is that are online when someone is watching via www like see a webpage. And yes is random not always.
0
 
anarki_jimbelCommented:
Just some sample code (written really in WinForm app).

BTW, you need to check for other errors like wrong index etc.
You may also introduce some delay for each iteration.

        private void button1_Click(object sender, EventArgs e)
        {
            lineChanger("QWERTY", "MyTextFile.txt", 2);
        }

        static void lineChanger(string newText, string fileName, int line_to_edit)
        {
            int attemptCounter = 0;
            for (attemptCounter = 1; attemptCounter <= 10; attemptCounter++)
            {
                try
                {
                    System.Diagnostics.Debug.WriteLine("Attempt " + (attemptCounter));
                    string[] arrLine = File.ReadAllLines(fileName);
                    arrLine[line_to_edit - 1] = newText;
                    File.WriteAllLines(fileName, arrLine);

                    // No error - break
                    break;
                }
                catch (IOException ioex)
                {
                    System.Diagnostics.Debug.WriteLine("IOException happened: " + ioex.ToString());
                    if (attemptCounter >= 10)
                    {
                        MessageBox.Show("File is in use");
                    }
                }
            }
        }

Open in new window

0
 
Alex E.Author Commented:
Thank you so much is what we needed.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now