2013-05-07 56 views
0

我相信創建文件已完成。我遇到保存到文件的問題。我應該做的是最初創建文件,然後填寫表單並將其保存到該文件,並通過文件中的逗號分隔它們,這樣在我的下一個作業中,我可以創建一個表單來讀取文件並填充該文件在表格中並由這些逗號分開並填入文本框中。C#在創建文件後從Windows窗體保存

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 

namespace FileExercise 
{ 
    public partial class frmscout : Form 
    { 
     private StreamWriter fileWriter; 

     public frmscout() 
     { 
      InitializeComponent(); 
      btnsave.Enabled = false; 
     } 

     private void clickclear(object sender, EventArgs e) 
     { 
      tb40.Clear(); 
      tbheight.Clear(); 
      tbname.Clear(); 
      tbposition.Clear(); 
      tbreps.Clear(); 
      tbverticle.Clear(); 
     } 

     private void clickexit(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 

     private void clickselect(object sender, EventArgs e) 
     { 
      DialogResult result; 
      string fileName; 


      using (SaveFileDialog fileChooser = new SaveFileDialog()) 
      { 
       fileChooser.CheckFileExists = false; 
       result = fileChooser.ShowDialog(); 
       fileName = fileChooser.FileName; 
      } 

      if (result == DialogResult.OK) 
      { 
       if (fileName == string.Empty) 
       { 
        MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       } 
       else 
       { 
        try 
        { 
         FileStream flstrm = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); 
         fileWriter = new StreamWriter(flstrm); 

         btnsave.Enabled = true; 
         btnopen.Enabled = false; 
        } 
        catch(IOException) 
        {enter code here 
         MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
        } 
       } 
      } 



      //FileStream outfile = new FileStream(fileName, FileMode.Append, FileAccess.Write); 


     } 

     private void clicksave(object sender, EventArgs e) 
     { 
      string[] values = new string[6]; 

      values[0] = tbname.Text; 
      values[1] = tbheight.Text; 
      values[2] = tb40.Text; 
      values[3] = tbposition.Text; 
      values[4] = tbreps.Text; 
      values[5] = tbverticle.Text; 
      //}     
     } 
    } 
} 

回答

0

你正在創建的StreamWriter的新實例,並收集數據的保存按鈕的onClick,但無處你叫StreamWriter.Write()StreamWriter.WriteLine()。您至少需要在代碼中插入其中一個,這很可能在clicksave函數中。

編輯: 例如:

private void clicksave(object sender, EventArgs e) 
{ 
    string[] values = new string[6]; 

    values[0] = tbname.Text; 
    values[1] = tbheight.Text; 
    values[2] = tb40.Text; 
    values[3] = tbposition.Text; 
    values[4] = tbreps.Text; 
    values[5] = tbverticle.Text; 

    fileWriter.WriteLine(String.Join(",",values)); 
    fileWriter.Flush(); 
    fileWriter.Close(); 
    //}     
} 
0

你可以做如下

private void clicksave(object sender, EventArgs e) 
    { 
     string[] values = new string[6]; 

     values[0] = tbname.Text; 
     values[1] = tbheight.Text; 
     values[2] = tb40.Text; 
     values[3] = tbposition.Text; 
     values[4] = tbreps.Text; 
     values[5] = tbverticle.Text; 
     // you can get file name from `ShowDialog`, 
     //assume that file name is "filename.txt" then 
     System.IO.File.WriteAllLines("filename.txt",values);     
    } 
0
Private void clicksave(object sender, EventArgs e) 
{ 
string[] values = new string[6]; 

values[0] = tbname.Text; 
values[1] = tbheight.Text; 
values[2] = tb40.Text; 
values[3] = tbposition.Text; 
values[4] = tbreps.Text; 
values[5] = tbverticle.Text; 

fileWriter.WriteLine(String.Join(",",values)); 
fileWriter.Flush(); 
fileWriter.Close(); 

} 

你可以參考以下鏈接字符串的加盟....因爲這來自艾德里安代碼..

http://www.dotnetperls.com/string-join

+0

工作。謝謝。 – 2013-05-07 20:20:14