Solved

File.WriteAllLines problem at random C# ASP.NET

Posted on 2016-09-19
6
52 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
Comment Utility
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
Comment Utility
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.
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Alex E.
Comment Utility
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
Comment Utility
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.
Comment Utility
Thank you so much is what we needed.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

10 Experts available now in Live!

Get 1:1 Help Now