SaveFileDialog控件存储文件

//事件代码

private void button1_Click(object sender, EventArgs e)
        {
            sfd_File.Title = "存储数据文件";
            sfd_File.Filter = "文本文件(*.txt)|*.txt";
            sfd_File.RestoreDirectory = true;
            if (sfd_File.ShowDialog() == DialogResult.OK)
            {
                if (sfd_File.ShowDialog() == DialogResult.OK)
                {
                    string fName = sfd_File.FileName;
                    File fSaveAs = new File(fName);
                    fSaveAs.WriteFile(txt_GetResp.Text);
                }

                else
                {
                    MessageBox.Show("请命名文件!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

//类方法

 

public class File
        {
            string fileName;
            public File(string fileName)
            {
                this.fileName = fileName;
            }

            public string ReadFile()
            {
                try
                {
                    StreamReader sr = new StreamReader(fileName, Encoding.Default);
                    string result = sr.ReadToEnd();
                    sr.Close();
                    return result;
                }
                catch (Exception e) { MessageBox.Show(e.Message); }
                return null;
            }
            public void WriteFile(string str)
            {
                try
                {
                    StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default);
                    sw.Write(str);
                    sw.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "保存文件出错!");
                }
            }
        }

 

 

posted @ 2009-03-30 10:38  迪卡.凯恩  阅读(594)  评论(0编辑  收藏  举报