zoukankan      html  css  js  c++  java
  • c# winform 路径选择和文件读写

    //读文件
            private void readBtn_Click(object sender, EventArgs e)
            {
                try 
                {
                    if (pathTxt.Text == "")
                    {
                        MessageBox.Show("请输入文件地址");
                        return;
                    }
                    readTxt.Text = File.ReadAllText(pathTxt.Text, Encoding.Default);//不须要对地址中的""进行转义
                }
                catch (Exception ex)
                {
                    MessageBox.Show("文件地址错误");
                }
               
                
            }
            //写文件
            private void writeBtn_Click(object sender, EventArgs e)
            {
                try
                {
                    if (pathTxt.Text == "")
                    {
                        MessageBox.Show("请输入文件地址");
                        return;
                    }
                    File.WriteAllText(pathTxt.Text, writeTxt.Text);
                    MessageBox.Show("success");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("文件地址错误");
                }
            }
            //读取目录路径
            private void folderBtn_Click(object sender, EventArgs e)
            {
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    folderTxt.Text = folderBrowserDialog1.SelectedPath;
                }
            }
            //读取文件路径
            private void fileBtn_Click(object sender, EventArgs e)
            {
                
                OpenFileDialog op = new OpenFileDialog();
                op.Filter = "Text files (*.txt)|*.txt|All Files(*.*)|*.*";
                if (op.ShowDialog() == DialogResult.OK)
                {
                    pathTxt.Text = op.FileName;
                }
            }

  • 相关阅读:
    VS2013快速安装教程
    软件工程课程的感想
    GitHub和Microsoft TFS对比有什么优势
    拼接素数
    C语言程序题
    vue中的实例方法的底层原理
    ios 安卓
    防抖
    伪数组转真数组的放法
    http和https的一种能力?
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5105285.html
Copyright © 2011-2022 走看看