2012-01-16 2 views
-2

현재 열려있는 파일을 저장하지 않고 대화 상자를 팝업하지 않고 저장하는 데 문제가 있습니다.프롬프트없이 현재 열린 파일 저장

좀 더 명확히하기 위해 .txt 파일을 열고 작업 한 다음 '저장'을 클릭하고 '다른 이름으로 저장'대화 상자를 팝업하지 않고 파일을 저장하려고합니다.

여기 내 저장 코드 :

 private void SaveFile() 
    { 
     SaveFileDialog fileChooser = new SaveFileDialog(); 
     fileChooser.Title = "Choose Save Location"; 
     fileChooser.Filter = "Text Files (*.txt)|*.txt"; 

     fileChooser.OverwritePrompt = false; //Removes warning 

     DialogResult result = fileChooser.ShowDialog(); 

     if (result == DialogResult.Cancel) 
     { 
      return; 
     } 

     try 
     { 
      string fileName = fileChooser.FileName; 
      output = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); 

      fileWriter = new StreamWriter(output); 

      foreach (Employee emp in employee) 
      { 
       fileWriter.WriteLine(emp.Firstname + "," + emp.Lastname + "," + emp.Position + "," + emp.Bmonth + "," + emp.Bday + "," + emp.BYear + "," + emp.Salary + "," + emp.Hiremonth + "," + emp.Hireday + "," + emp.Hireyear); 
      } 

      fileWriter.Close(); 
      output.Close(); 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      fileWriter.Close(); 
      output.Close(); 
     } 
    } 

모든 위대한까지 .txt 파일에 저장하고이를 다시로드로 작동, 그것은 나를 irks 그 팝업이 단지.

+1

, 당신은 어떻게 건너에 대해 fileChooser 대화 상자를 모두 열고 그냥 저장 하시겠습니까? 문제가 보이지 않습니다. –

+2

코드에 파일 저장 대화 상자가 표시됩니다. 네가 원하지 않으면 ...하지 마라. –

+0

파일을 열고 변경 한 경우 도구 모음에서 '저장'을 클릭하고 저장하려면 이름을 묻는 대신 파일을 저장하면됩니다. –

답변

2

fileChooser 개체는 SaveFileDialog 개체입니다. 당신은 호출하여 표시하는 데 원인이있어 : 당신이 대화 상자를 표시하지 않으려면

DialogResult result = fileChooser.ShowDialog(); 

, 단지 사용하는 대신 fileChooser 코드를 생략하고 :

string fileName = strAlreadyKnownFileName; 
0

내가 먼저 저장하려는

private string filepath = "path/to/my/file";

이 그럼 당신은 즉, 버튼 w을 더블 클릭 "저장"버튼을 생성하고 호출해야합니다 몇 가지 변수에 열려있는 파일의 전체 경로는 말할 수 현재 열려있는 파일에 당신이 원하는대로 저장이 간단한 코드를 라이트 : 그렇게 간단

을 ...

편집 : 이미 파일 이름이있는 경우

private void SaveFile() 
{ 
    //do your loop and stuff in here and finally write your text to the file using this 
    File.WriteAllText(filepath, yourtexttobesaved); 
}