2011-08-16 9 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はあなたの名前、FileNameプロパティを提供します。それはあなたのための節約をしません。あなたの仕事。 –

答えて

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

関連する問題