English 中文(简体)
IOError 开标
原标题:IOError opening log file
  • 时间:2011-03-31 11:42:46
  •  标签:
  • c#

I m writing a program that parses the log files of a Windows service. The program works just fine, it can read and parse correctly log files and so on. The problem is, when I try to run it with the service active, it returns a IOError, I guess because the files are "locked" or something like that. Is there a workaround? This is the code I use to read and parse the files:

private void OnTimedEvent(object source, ElapsedEventArgs e)
{
    var t = Task<int>.Factory.StartNew(() => 
        {
            string[] arr = FileManager.getFiles(textBox1.Text);
            string destination = textBox1.Text + "\backup\";
            string filename = "backup.bak";

            if (!Directory.Exists(@destination))
            {
                Directory.CreateDirectory(@destination);
            }

            StreamWriter w = File.AppendText(@destination+backupfile);

            foreach (string s in arr)
            {
                string sLine = "";
                StreamReader objReader = new StreamReader(s);
                while (sLine != null)
                {
                    sLine = objReader.ReadLine();
                    if (!String.IsNullOrEmpty(sLine))
                    {
                        List<string> output = Parser.parse(sLine);
                        StreamWriter hh = File.AppendText(@textBox2.Text + filename);

                        for(int i = 3; i < output.Count; i++)
                        {
                            string str = output[i];

                            if (!String.IsNullOrEmpty(str))
                            {
                                hh.WriteLine(str);
                            }
                        }
                        hh.Close();
                        w.WriteLine(sLine);
                    }
                }
                objReader.Close();
                //File.Delete(s);
            }
            w.Close();
            return 1;
        });
}
最佳回答

替换

<编码>新软件

iii

new StreamReader(File.OpenRead(s)) or

<代码>Reader(新文件Stream(s,DileMode.Open)

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签