zoukankan      html  css  js  c++  java
  • 文件操作——c#

    1.OpenFileDialog

    private void btnOpen_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.InitialDirectory = @"C:UsersLWP1398Desktop"; //设置初始路径
                ofd.Filter = "Excel文件(*.xls)|*.xls|Csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //设置“另存为文件类型”或“文件类型”框中出现的选择内容
                ofd.FilterIndex = 2; //设置默认显示文件类型为Csv文件(*.csv)|*.csv
                ofd.Title = "打开文件"; //获取或设置文件对话框标题
                ofd.RestoreDirectory = true;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    //FileName:所选文件的全路径  SafeFileName:所选的文件名
                    txtPath.Text = "FileName:" + ofd.FileName + "
    " + "SafeFileName:" + ofd.SafeFileName;
                }
            }

    2.OpenFileDialog选择多个文件

    private void button3_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.InitialDirectory = @"C:UsersLWP1398Desktop"; //设置初始路径
                ofd.Filter = "Excel文件(*.xls)|*.xls|Csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //设置“另存为文件类型”或“文件类型”框中出现的选择内容
                ofd.FilterIndex = 2; //设置默认显示文件类型为Csv文件(*.csv)|*.csv
                ofd.Title = "打开文件"; //获取或设置文件对话框标题
                ofd.RestoreDirectory = true;设置对话框是否记忆上次打开的目录
     
                ofd.Multiselect = true;//设置多选
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    for (int i = 0; i < ofd.FileNames.Length; i++)
                    {
                        txtPath.Text += ofd.FileNames[i] + "
    ";//输出一个路径回车换行
                    }
                    for (int i = 0; i < ofd.FileNames.Length; i++)
                    {
                        txtPath.Text += ofd.SafeFileNames[i] + "
    ";
                    }
                }
            }

    3.SaveFileDialog

    private void button2_Click(object sender, EventArgs e)
            {
                SaveFileDialog sfd=new SaveFileDialog();
                sfd.Filter = "文本文件(*.txt)|*.txt|所有文件|*.*";//设置文件类型
                sfd.FileName = "保存";//设置默认文件名
                sfd.DefaultExt = "txt";//设置默认格式(可以不设)
                sfd.AddExtension = true;//设置自动在文件名中添加扩展名
                if (sfd.ShowDialog()==DialogResult.OK)
                {
                    txtPath.Text = "FileName:" + sfd.FileName + "
    " ;
                    using (StreamWriter sw = new StreamWriter(sfd.FileName))
                    {              
                        sw.WriteLineAsync("今天是个好天气");
                    }
                }
                MessageBox.Show("ok");
            }

     

    private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
    {
         saveFileDialog1.AddExtension = true; //自动添加扩展名
         e.Cancel = true; //取消保存操作            
         string 扩展名 = System.IO.Path.GetExtension(saveFileDialog1.FileName);
         //判断扩展名并实现自定义的保存操作(导出)
         if (扩展名 == "txt")
         { }
         if (扩展名 == "xml")
         { }
    }

    4.FolderBrowserDialog

    string defaultPath = "";
    FolderBrowserDialog dialog = new FolderBrowserDialog();
    //打开的文件夹浏览对话框上的描述
    dialog.Description = "请选择一个文件夹";
    //是否显示对话框左下角 新建文件夹 按钮,默认为 true
    dialog.ShowNewFolderButton = false;
    //首次defaultPath为空,按FolderBrowserDialog默认设置(即桌面)选择
    if (defaultPath != "")
    {
        //设置此次默认目录为上一次选中目录
        dialog.SelectedPath = defaultPath;
    }
    //按下确定选择的按钮
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        //记录选中的目录
        defaultPath = dialog.SelectedPath;
    }
    MessageBox.show(defaultPath);

    文章转自:https://blog.csdn.net/u011108093/article/details/81627935

  • 相关阅读:
    获取汉字和字母的长度
    JavaScript 获取对象中第一个属性
    JS函数的length属性
    原型模式 -- JavaScript语言的灵魂
    建造者模式
    阻止form元素内的input标签回车提交表单
    工厂方法模式(安全的工厂方法)
    微信自研生产级paxos类库PhxPaxos实现原理介绍
    图解分布式一致性协议Paxos
    Paxos协议超级详细解释+简单实例
  • 原文地址:https://www.cnblogs.com/eve612/p/14232283.html
Copyright © 2011-2022 走看看