Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 打开日志文件时出错_C# - Fatal编程技术网

C# 打开日志文件时出错

C# 打开日志文件时出错,c#,C#,我正在编写一个程序,解析Windows服务的日志文件。该程序运行良好,可以正确读取和解析日志文件等。 问题是,当我尝试在服务处于活动状态的情况下运行它时,它会返回一个IOError,我猜是因为文件被“锁定”或类似的东西。有解决办法吗?这是我用来读取和解析文件的代码: private void OnTimedEvent(object source, ElapsedEventArgs e) { var t = Task<int>.Factory.StartNew(() =>

我正在编写一个程序,解析Windows服务的日志文件。该程序运行良好,可以正确读取和解析日志文件等。 问题是,当我尝试在服务处于活动状态的情况下运行它时,它会返回一个IOError,我猜是因为文件被“锁定”或类似的东西。有解决办法吗?这是我用来读取和解析文件的代码:

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;
        });
}
private void OnTimedEvent(对象源,ElapsedEventArgs e)
{
var t=Task.Factory.StartNew(()=>
{
字符串[]arr=FileManager.getFiles(textBox1.Text);
string destination=textBox1.Text+“\\backup\\”;
字符串filename=“backup.bak”;
如果(!Directory.Exists(@destination))
{
CreateDirectory(@destination);
}
StreamWriter w=File.AppendText(@destination+backupfile);
foreach(arr中的字符串s)
{
字符串sLine=“”;
StreamReader objReader=新的StreamReader;
while(sLine!=null)
{
sLine=objReader.ReadLine();
如果(!String.IsNullOrEmpty(sLine))
{
列表输出=Parser.parse(sLine);
StreamWriter hh=File.AppendText(@textBox2.Text+filename);
for(int i=3;i
更换

新的StreamReader

newstreamreader(File.OpenRead))


newstreamreader(newfilestream(s,FileMode.Open))

请给出您在生产中遇到的异常,仅仅是一个IO错误就可能意味着很多事情。已使用文件,未找到文件。。。等等