码迷,mamicode.com
首页 > Windows程序 > 详细

c# winform 路径选择和文件读写

时间:2014-06-14 11:17:07      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:winform   folderbrowserdialog   openfiledialog   file   

//读文件
        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;
            }
        }

c# winform 路径选择和文件读写,布布扣,bubuko.com

c# winform 路径选择和文件读写

标签:winform   folderbrowserdialog   openfiledialog   file   

原文地址:http://blog.csdn.net/xyl295528322/article/details/30541455

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!