Solved

File.WriteAllLines problem at random C# ASP.NET

Posted on 2016-09-19
6
65 Views
Last Modified: 2016-09-20
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
Comment
Question by:Alex E.
  • 3
  • 2
6 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 41805763
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
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41805767
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
 

Author Comment

by:Alex E.
ID: 41805768
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Alex E.
ID: 41805775
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
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 41805786
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
 

Author Closing Comment

by:Alex E.
ID: 41806517
Thank you so much is what we needed.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now