Question

In a c# program, I need to read a string in a file after every "n" seconds.My program code is like below,

start:

.
.
.
config_path = Regex.Match(System.IO.File
                   .ReadAllText("c:\docs\xyz.log"),".*Command.*File:.*")
       .ToString()
       .Split(new string[] { "File: ", " ...\r" },                
                         StringSplitOptions.RemoveEmptyEntries)
       .Last();
.
.
.


goto start;

For the first time in the loop , the program executes fine.But for the second entry into the label (start), the exception "c:\docs\xyz.log" is thrown. How can i unlock this file for every entry into the label.

Was it helpful?

Solution

If you're using the same text from the same file over and over again - you'd better read it into a string before the loop starts and then use the string over and over again:

string str = System.IO.File.ReadAllText("c:\docs\xyz.log");
...
// now use str in the loop
start:
...

Comment:
It's a bad practice to write so many commands on the same line, when you'll get an error you wouldn't know where it's coming from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top