-1

I already have a windows service with a filesystemwatcher that checks for any file that has been changed (e.g. size). If a logfile has reached 1mb, it will be copied into another folder and the original logfile will be cleared.

I made a windows form project that has a button that simulates what my windows service is doing. I tried creating a forloop that will check the size of multiple textfiles. I created 3 logfiles that exceeds 1mb and ran my project. It gave a "The device is not ready" error, it's with the StreamWriter. I think that all the files are being read and backed up at the same time.

To sum it all up: How can I make it not backup all the files simultaneously? I read about processes but I think its for executables only, am I right?

4

1 回答 1

0

您是否尝试过这种方式:

    if(!File.Exists(filename)) //No File? Create
{
    fs = File.Create(filename);
    fs.Close();
}
if(File.ReadAllBytes().Lenght >= 100*1024*1024) // (100mB) File to big? Create new
{
    string filenamebase = "myLogFile"; //Insert the base form of the log file, the same as the 1st filename without .log at the end
    if(filename.contains("-")) //Check if older log contained -x
    {
         int lognumber = Int32.Parse(filename.substring(filename.lastIndexOf("-")+1, filename.Length-4); //Get old number, Can cause exception if the last digits aren't numbers
         lognumber++; //Increment lognumber by 1
         filename = filenamebase + "-" + lognumber + ".log"; //Override filename
    }
    else 
    {
         filename = filenamebase + "-1.log"; //Override filename
    }
    fs = File.Create(filename);
    fs.Close();
}
于 2012-08-21T17:46:28.173 回答