2012-05-16 4 views
0

formclosing 이벤트의 텍스트 파일에 일부 문자열을 쓰려고합니다. 문제는 streamwriter가 아무 것도 쓰지 않는다는 것입니다. 빈 슬레이트 만 씁니다. 나는 2 개의 다른 텍스트 파일을 가지고 있는데 첫 번째 파일은 모든 그래프 데이터를 기록하고 두 번째 텍스트 파일은 내 응용 프로그램과 관련된 몇 가지 기본 설정을 기록합니다. 내 코드는 closing 이벤트 및 별도의 주력 방법 모두에 대해 다음과 같습니다양식 닫기 이벤트에서 텍스트 파일에 쓰기

private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 


     if (e.CloseReason.Equals(CloseReason.WindowsShutDown) || (e.CloseReason.Equals(CloseReason.UserClosing))) 
     { 
      if (MessageBox.Show("You are closing this application.\n\nAre you sure you wish to exit ?", "Warning: Not Submitted", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Stop) == DialogResult.Yes) 
      { 

       writeContents("Interrupted"); 

       return; 
      } 

      else 
       e.Cancel = true; 
     } 



    } 

    private void writeContents(string status) 
    { 

     //---writes the graph data----- 
     TextWriter twBackupData = new StreamWriter("C://springTestBackupData.txt"); 

     twBackupData.WriteLine("--Cycle#-- --TorqueLower-- --TorqueUpper--"); 

     //writes the table of values in there, assume x and y are the same size arrays 
     for(int i = 0; i < x.Count; i++) 
     {    
      twBackupData.WriteLine(x[i] + " " + y_lower[i] + " " + y_upper[i]); 
     } 


     //---writes some of the preferences------ 
     TextWriter twBackupDataInfo = new StreamWriter("C://springTestBackupInfo.txt"); 

     twBackupDataInfo.WriteLine(status); 
     twBackupDataInfo.WriteLine(cycleCount.ToString()); 
     twBackupDataInfo.WriteLine(section.ToString()); 
     twBackupDataInfo.WriteLine(revsPerCycle.ToString()); 
     twBackupDataInfo.WriteLine(preturns.ToString()); 
     twBackupDataInfo.WriteLine(direction.ToString()); 

    } 

당신이 조언을 제공하거나 내가 대단히 감사하겠습니다 공백을 쓰고 왜 나를 찾을 수 있도록 할 수 있습니다. 고맙습니다!

+0

다음 StreamWriter.Flush()와 StreamWriter.Close을 (시도) –

+3

나는'.Close' 실제로뿐만 아니라'.Flush'를 호출 생각합니다. –

답변

2

using 문을 사용하여 StreamWriter을 닫아야합니다.

0

StreamWriters에서 .Close()을 수행해야합니다. 그것은 훨씬 쉽게

1

바로 사용 :

var linesToWrite = new list<string>(); 

linesToWrite.Add(status); 
linesToWrite.Add(cycleCount.ToString()); 
... 

File.WriteAllLines("C://springTestBackupData.txt", linesToWrite); 
1

당신은/닫기가 쓰기 위해 작가를 폐기해야하는, 그렇지 않은 경우는 스트림 (즉, 파일에 데이터를 기록)

를 플러시하지

using(TextWriter twBackupData = new StreamWriter("C://springTestBackupData.txt")) 
{ 
    // Do your stuff here - write to the tw --- 


    twBackupData.WriteLine("--Cycle#-- --TorqueLower-- --TorqueUpper--"); 

    //writes the table of values in there, assume x and y are the same size arrays 
    for(int i = 0; i < x.Count; i++) 
    {     
     twBackupData.WriteLine(x[i] + " " + y_lower[i] + " " + y_upper[i]); 
    } 
} 

파일이

에 기록됩니다 보장합니다 : 그렇게 범위를 벗어나면 자동으로 '사용'문을 사용하여 객체의 처분 여기

상세 정보 :

http://msdn.microsoft.com/en-us/library/yh598w02.aspx