SaveFileDialog而不是硬编码保存方向

Posted

技术标签:

【中文标题】SaveFileDialog而不是硬编码保存方向【英文标题】:SaveFileDialog instead of hardcoding save direction 【发布时间】:2017-02-07 20:09:58 【问题描述】:

好的,我已经做到了这一点,但现在我有点卡住了。

我创建了一个用于保存和加载学生成绩的 Windows 窗体。问题是保存和加载方向是硬编码的,我想使用文件对话框来保存和加载文件。

我不确定该怎么做。有什么想法吗?

private void btnLoad_Click(object sender, EventArgs e)
    
        string filePath = @"C:\Users\grades.txt";
        if (File.Exists(filePath))
        
            StreamReader stream = new StreamReader(filePath);
            txtResult.AppendText(stream.ReadToEnd());
            lblStatus.Text = "File Loaded";
        
        else
            MessageBox.Show("There was a problem loading the file");
    

    private void btnSave_Click(object sender, EventArgs e)
    
        lblStatus.Text = "Entry saved";//shows in status label

        //string that specifies the location of the .txt file
         string filePath = @"C:\Users\grades.txt";

         StreamWriter fileWriter = new StreamWriter(filePath, true);//creates new object of class StreamWriter to be able to write to file

        //enters the information from the different textboxes to the file
        fileWriter.WriteLine(txtLastName.Text + ", " + txtFirstName.Text + ":\t" + Convert.ToString(txtID.Text) +
            "\t" + txtClass.Text + "\t" + txtGrades.Text);

        fileWriter.Close();//closes filewriter

    


编辑: 有改进的新代码(我还没有实现 Aybe 的建议)。

我觉得我有点智障,但为什么这不起作用?在我看来,这应该有效,但事实并非如此。当我尝试加载文件时没有任何反应......

using System;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication14

public partial class Form1 : Form

    public Form1()
    
        InitializeComponent();
    

    private void btnOpen_Click(object sender, EventArgs e)
    
        var dlg = new OpenFileDialog();

        dlg.InitialDirectory = "c:\\";
        dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        dlg.FilterIndex = 2;
        dlg.RestoreDirectory = true;

        if (dlg.ShowDialog() != DialogResult.OK)
            return;

    


    private void btnSave_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)
        
            Stream myStream;
            if ((myStream = saveFileDialog1.OpenFile()) != null)
            
                StreamWriter fileWriter = new StreamWriter(myStream);//creates new object of class StreamWriter to be able to write to file

                //enters the information from the different textboxes to the file
                fileWriter.WriteLine(txtLastName.Text + ", " + txtFirstName.Text + ":\t" + Convert.ToString(txtID.Text) +
                    "\t" + txtClass.Text + "\t" + txtGrades.Text);

                fileWriter.Close();//closes filewriter
                myStream.Close();
            
        

    

【问题讨论】:

【参考方案1】:

Open 和 Save 对话实际上很简单。

首先,您将初始化对话框:

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)
         
             // Code to write the stream goes here.
             myStream.Close();
         
     

【讨论】:

愿意解释我的反对票和 Aybe 的回答吗?【参考方案2】:

除了@bwoogie 回答,利用using 关键字轻松处理资源:

using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1

    public partial class Form1 : Form
    
        public Form1()
        
            InitializeComponent();
        

        private void buttonOpen_Click(object sender, EventArgs e)
        
            using (var dialog = new OpenFileDialog())
            
                if (dialog.ShowDialog() != DialogResult.OK)
                    return;

                using (var reader = new StreamReader(dialog.OpenFile()))
                
                    // TODO read file
                
            
        

        private void buttonSave_Click(object sender, EventArgs e)
        
            using (var dialog = new SaveFileDialog())
            
                if (dialog.ShowDialog() != DialogResult.OK)
                    return;

                using (var writer = new StreamWriter(dialog.OpenFile()))
                
                    // TODO save file
                
            
        
    

【讨论】:

以上是关于SaveFileDialog而不是硬编码保存方向的主要内容,如果未能解决你的问题,请参考以下文章

不使用 SaveFileDialog 保存文件

Winform SaveFileDialog Word

从硬编码数组而不是 DB 创建游标

“字节码”而不是硬编码着色器性能

为啥当我使用 .map 而不是硬编码来动态填充内容时,Material UI 选项卡停止工作?

为啥库需要硬编码矢量化而不是编译器自动矢量化