创建文件后从 Windows 窗体保存 C#

Posted

技术标签:

【中文标题】创建文件后从 Windows 窗体保存 C#【英文标题】:C# Saving from a Windows form after Creating the File 【发布时间】:2013-05-07 03:51:25 【问题描述】:

我相信创建文件已完成。我在保存到文件时遇到问题。我应该做的是最初创建文件然后填写表格并将其保存到该文件并在文件中用逗号分隔它们,以便在我的下一个作业中我可以创建一个表格来读取文件并填写在表格中,用逗号分隔并填写到文本框中。

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;
            //                   
        
    

【问题讨论】:

【参考方案1】:

你可以这样做

    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);                   
    

【讨论】:

【参考方案2】:

您正在创建 StreamWriter 的新实例,并在单击“保存”按钮时收集数据,但您没有在任何地方调用 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();
    //                   

【讨论】:

【参考方案3】:
 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();

   

对于字符串的加入,您可以参考下面的链接....因为这是来自 Adrian Code 的参考..

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

【讨论】:

以上是关于创建文件后从 Windows 窗体保存 C#的主要内容,如果未能解决你的问题,请参考以下文章

将 Windows 窗体列表框保存到文本文件 C#

在 C# windows 窗体中制作的保存/加载控件

C#父子窗体怎么弄

C# Winform主窗体和子窗体之间如何相互操作对方的控件

C#如何创建一张access 表格

2个Windows服务C#之间的管道