Solved

File.WriteAllLines problem at random C# ASP.NET

Posted on 2016-09-19
6
103 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.
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 30

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 52

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 30

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

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.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

695 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