2012-09-13 37 views
-1

第三方應用程序正在讀取我的Windows應用程序的輸出文本文件,它正在暫停。我想,這是因爲文件在寫入或讀取時被鎖定。我怎麼能讓這個工作正常。請看我的代碼。C#中的讀寫操作鎖定文件

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

    System.Timers.Timer timer; 

    string currentfilename; 
    string currentcontent; 

    public Service1() 
    { 

//

 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); 
     } 




    } 
+0

不可能回答這個問題。 –

+0

爲什麼?問題不清楚? – suhail

+0

可能有一百萬個原因爲什麼會發生這種情況。 –

回答

0

這裏的問題是與正在讀我的應用程序輸出input.it沒有允許訪問一個以上的文件內容的第三方應用程序..