C#,Winform 文件的导入导出 File

1、导入

导入对话框:OpenFileDialog

private void sbtnsb_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "导入文件包(*.bak)|*.bak";//扩展名
                ofd.FileName = "导入的文件包名称";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string savePath = System.AppDomain.CurrentDomain.BaseDirectory + "data\reportData\";//要导出文件的路径
                    if (!File.Exists(savePath))
                    {
                        string saveName = ofd.FileName.Substring(ofd.FileName.LastIndexOf("\") + 1, ((ofd.FileName.IndexOf(".bak") - 1) - ofd.FileName.LastIndexOf("\")));
                        string dataPath = savePath + saveName + ".mdb";//文件地址
                        string dataName = saveName + ".mdb";//文件名
                        File.Copy(ofd.FileNames[0], dataPath, true);
            MessageBox.Show("导入成功!");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("导入.bak文件错误信息:" + ex.Message);
            }
        }

2、导出

导出对话框:SaveFileDialog

private void sbtndc_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "导出文件包(*.baks)|*.baks";//扩展名
                sfd.FileName = "这是导出的文件包";//导出文件包文件名

                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    string dataPath = System.AppDomain.CurrentDomain.BaseDirectory + "data\skymxm.mdb";
                    if (File.Exists(dataPath))
                    {
                        File.Copy(dataPath, sfd.FileNames[0], true);
                        MessageBox.Show("导出成功!");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("导出.bak文件错误信息:" + ex.Message);
            }
        }

 

原文地址:https://www.cnblogs.com/pingming/p/4221807.html