Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
Windows 文件在C中读/写操作时被锁定#_Windows_C# 4.0 - Fatal编程技术网

Windows 文件在C中读/写操作时被锁定#

Windows 文件在C中读/写操作时被锁定#,windows,c#-4.0,Windows,C# 4.0,第三方应用程序正在读取我的windows应用程序的输出文本文件,该文件正在暂停。我认为,发生这种情况是因为文件在写入或读取时被锁定。我如何才能使其正常工作。请查看我的代码 List<string> fileList = new List<string>(); System.Timers.Timer timer; string currentfilename; string currentcontent; public Serv

第三方应用程序正在读取我的windows应用程序的输出文本文件,该文件正在暂停。我认为,发生这种情况是因为文件在写入或读取时被锁定。我如何才能使其正常工作。请查看我的代码

    List<string> fileList = new List<string>();

    System.Timers.Timer timer;

    string currentfilename;
    string currentcontent;

    public Service1()
    {

这里的问题是第三方应用程序将我的应用程序输出作为输入读取。它不允许访问多个文件内容。

不可能回答此问题。出现这种情况的原因可能有一百万个。我的应用程序应该从扩展名为的目录中新更新/创建的文件中复制内容.jrn到文本文件。此文本文件正在由一个名为POS text Sender的应用程序读取。上述编写的代码工作正常,但POS text Sender将从中间停止读取。如果上述代码无法实现其目标,您能否告诉我如何以其他方式创建此应用程序。。
        timer = new System.Timers.Timer();

        timer.AutoReset = false;



        timer.Elapsed += new System.Timers.ElapsedEventHandler(DoStuff);

    }

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        throw new NotImplementedException();
    }

    private void DoStuff(object sender, System.Timers.ElapsedEventArgs e)
    {


     DateTime LastChecked = DateTime.Now;
     try
     {
         string[] files = System.IO.Directory.GetFiles(@"C:\Journal", "*.jrn", System.IO.SearchOption.AllDirectories);

         foreach (string file in files)
         {
             if (!fileList.Contains(file))
             {
                 currentfilename = file;
                 fileList.Add(file);

                     copywarehouse(file);


                 try
                 {


                         string sourcePath = @"C:\Journal";
                         string sourceFile = System.IO.Path.Combine(sourcePath, file);
                         using (FileStream fs= new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                         {
                             StreamReader sr = new StreamReader(fs);
                             currentcontent = sr.ReadToEnd();
                             sr.Close();

                         }


                 }
                 catch (Exception ex)
                 {

                     throw (ex);

                 }


         }


         }

         try
             {


                     string sourcePath1 = @"C:\Journal";
             string currentfilecontent;
                     string sourceFile1 = System.IO.Path.Combine(sourcePath1, currentfilename);

                     using (FileStream fs = new FileStream(sourceFile1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                     {
                         StreamReader sr = new StreamReader(fs);
                         currentfilecontent = sr.ReadToEnd();
                         sr.Close();

                     }





                     if (currentfilecontent != currentcontent)
                     {
                         if (currentfilecontent.Contains(currentcontent))
                         {
                             string originalcontent = currentfilecontent.Substring(currentcontent.Length);


                                 File.WriteAllText(@"C:\Journal\tempfile.txt", originalcontent + "\r\n");
                              using (FileStream fs = new FileStream(@"C:\Journal\tempfile.txt", FileMode.OpenOrCreate))
            {
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(originalcontent);
                sw.Close();
            }

                             currentcontent = currentfilecontent;
                         }
                     }



                 }
             //}

                 catch (Exception ex)
                 {

                     throw (ex);
                 }

         TimeSpan ts = DateTime.Now.Subtract(LastChecked);
         TimeSpan MaxWaitTime = TimeSpan.FromMilliseconds(100);


         if (MaxWaitTime.Subtract(ts).CompareTo(TimeSpan.Zero) > -1)
             timer.Interval = MaxWaitTime.Subtract(ts).Milliseconds;
         else
             timer.Interval = 1;

         timer.Start();



     }



     catch (Exception ex)
     {
         throw (ex);
     }










    }
    private void copywarehouse(string filename)
    {
        string sourcePath = @"C:\Journal";
        string targetPath = @"C:\Journal";

        string copycontent=null;
        try
        {
            string sourceFile = System.IO.Path.Combine(sourcePath, filename);
            string destFile = System.IO.Path.Combine(targetPath, "tempfile.txt");


            using (FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
               StreamReader sr = new StreamReader(fs);

                copycontent = sr.ReadToEnd();
                sr.Close();


            }

            using (FileStream fs = new FileStream(destFile, FileMode.Append))
            {
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(copycontent);
                sw.Close();
            }



        }
        catch (Exception ex)
        {
            throw (ex);
        }




    }