2011-08-16 4 views
1

는 여기에 내가 현재 그것은 나 텍스트 파일을 열 수 있습니다 OpenFileDialog를`C#의 savefile 대화 상자에서 어떻게 저장합니까?

private void openToolStripMenuItem_Click_1(object sender, System.EventArgs e) 
    { 
     //opens the openfiledialog and gives the title. 
     openFileDialog1.Title = "openfile"; 
     //only opens files from the computer that are text or richtext. 
     openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
     //gets input from the openfiledialog. 
     if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      //loads the file and puts the content in the richtextbox. 
      System.IO.StreamReader sr = new 
    System.IO.StreamReader(openFileDialog1.FileName); 
      richTextBox1.Text = (sr.ReadToEnd()); 
      sr.Close();`                        here is the code I am using to save through a savefiledialog   ` 

    Stream mystream; 
    private void saveToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 

     saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
     saveFileDialog1.FilterIndex = 2; 
     saveFileDialog1.RestoreDirectory = true; 

     if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      if ((mystream = saveFileDialog1.OpenFile()) != null) 
      { 
       StreamWriter wText = new StreamWriter(mystream); 

       wText.Write(""); 

       mystream.Close(); 

` 을 사용하여 파일을 열 사용하고있는 코드입니다하지만 변경 사항을 저장하거나 내 자신의 텍스트 파일을 만들 수 없습니다. 런타임에는 오류가 표시되지 않습니다. 추가 도움을 주셔서 다시 한 번 감사드립니다.

+0

의 SaveFileDialog는 당신에게 이름을 제공, 파일 이름 속성입니다. 그것은 당신을 위해 저축을하지 않습니다. 작업. –

답변

12

SaveFileDialog은 실제 절약하지 않습니다. 단순히 사용자가 파일 경로를 지정할 수 있습니다. 당신은 파일 경로를 사용하고 StreamWriter class의 구현, 같은과 무거운 작업을 수행합니다

if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    using(Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew)) 
    using(StreamWriter sw = new TextWriter(s)) 
    { 
     sw.Write(someTextBox.Text); 
    } 
} 
+0

죄송합니다. 최근에 들어 오지 못했습니다. 웹에서 몇 가지 예를 찾고 난 당신이 말하는 것과 비슷한 것을 가지고 있습니다. http://zamov.online.fr/EXHTML/CSharp/CSharp_302155.html을 저장하고 http://msdn.microsoft.com/en-us/library/aa984392%28v=vs를 열어보기위한 링크가 있습니다. .71 % 29.aspx하지만 여전히 아이디어를 얻는 것처럼 보이지 않습니다. – mendez

+0

나를 위해 "[does not] work"를 정의해야합니다. –

+0

어떤 이유로 텍스트 파일을 열 수 있지만 뭔가를 입력하고 저장하면 파일이 표시되지 않습니다. – mendez

관련 문제