C#中 文件的打开及保存

1.  根据地址打开文件

  AppDomain.CurrentDomain.BaseDirectory  

        Application.StartupPath     // 根路径 :即进入到项目的Debug层  

    System.Diagnostics.Process.Start(@"Help\操作说明.docx"); //直接打开文档

2.  使用资源文件

  例如: Properties.Resources._48_label_24px_14018_easyicon_net  

4.动态保存

复制代码
#region 弹出保存文件夹选择窗口类
    //一般选择文件保存地址都用弹出对话框来进行选择
    public class DirectorySelect : FolderNameEditor
    {
        private FolderBrowser fb = new FolderBrowser();
        private string fDescription = "Choose Directory";
        private string fReturnPath = String.Empty;

        public string Description
        {
            set { fDescription = value; }
            get { return fDescription; }
        }

        public string ReturnPath
        {
            get { return fReturnPath; }
        }

        public DirectorySelect()
        {

        }
        private DialogResult RunDialog()
        {
            fb.Description = this.Description;
            fb.StartLocation = FolderBrowserFolder.MyComputer;
            fb.Style = FolderBrowserStyles.RestrictToSubfolders;
            //|FolderBrowserStyles.RestrictToDomain;
            return fb.ShowDialog();
        }

        public DialogResult ShowDialog()
        {
            DialogResult dRes = DialogResult.None;
            dRes = RunDialog();
            if (dRes == DialogResult.OK)
                this.fReturnPath = fb.DirectoryPath;
            else
                this.fReturnPath = String.Empty;
            return dRes;
        }
    }

    //一般选择文件保存地址都用弹出对话框来进行选择
    //调用   
    //DirBrowser   myDirBrowser=new   DirBrowser();   
    //if(myDirBrowser.ShowDialog()!=DialogResult.Cancel)   
    //MessageBox.Show(myDirBrowser.ReturnPath);   

    public class DirBrowser : FolderNameEditor
    {
        FolderBrowser fb = new FolderBrowser();
        public string Description
        {
            set { _description = value; }
            get { return _description; }
        }

        public string ReturnPath
        {
            get { return _returnPath; }
        }

        public DirBrowser() { }
        public DialogResult ShowDialog()
        {
            fb.Description = _description;
            fb.StartLocation = FolderBrowserFolder.MyComputer;
            DialogResult r = fb.ShowDialog();
            if (r == DialogResult.OK)
                _returnPath = fb.DirectoryPath;
            else
                _returnPath = String.Empty;

            return r;
        }

        //private   string   _description   =   "Choose   Directory";     
        //private   string   _returnPath   =   String.Empty;  
        private string _description = "请选择文件夹";
        private string _returnPath = String.Empty;
    }

    #endregion
View Code
复制代码
复制代码
//动态添加地址
DirectorySelect fileHlep = new DirectorySelect();
if (fileHlep.ShowDialog() == DialogResult.OK)
{
      FilePath = fileHlep.ReturnPath + dr["FileName"].ToString();
}
//保存

 FileStream fs2 = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
 fs2.Write(byData , 0, byData .Length);
 fs2.Close();
View Code
复制代码

 

posted @   马玲  阅读(4818)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示