?
Solved

File.WriteAllLines problem at random C# ASP.NET

Posted on 2016-09-19
6
Medium Priority
?
122 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 53

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

765 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