网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

关于OpenFileDialog的使用

http://www.diybl.com/ 2008-3-24  网络 点击:  [ 评论 ]
文章搜索:    点击制作chm打包电子书教程

为了方便同事在日常工作中很快速生成大量数据, 我做了文件拷贝的小工具:

其中用到了OpenFileDialog这个类,下面是关于这个类的一些用法!

OpenFileDialog类是用来选择文件位置的,

FolderBrowserDialog 类用来选择文件夹位置.

具体代码如下:

 

 

 

 

 

 

 

                                                            程序截图

程序源码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace copyFile
{
    public partial class Form1 : Form
    {
        String fileName;
        String folderName;
        String extendedName;
        String fileName1;
       
        public Form1()
        {
            InitializeComponent();
        }

        private void browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();                //new一个方法
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);  //定义打开的默认文件夹位置
            ofd.ShowDialog();          //显示打开文件的窗口
             fileName = ofd.FileName;               //获得选择的文件路径
             textBox1.Text = fileName;
             extendedName = Path.GetExtension(fileName);       //获得文件扩展名
             fileName1 = Path.GetFileName(fileName);           //获得文件名
        }

        private void folder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            folderName = fbd.SelectedPath;                     //获得选择的文件夹路径
            textBox3.Text = folderName;
        }

        private void ok_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim().Length == 0)
            {
                MessageBox.Show("文件路径不能为空!");
                return;
            }
            if (textBox2.Text.Trim().Length == 0)
            {
                MessageBox.Show("复制数量不能为空!");
                return;
            }
            if (textBox3.Text.Trim().Length == 0)
            {
                MessageBox.Show("目标文件夹路径不能为空!");
                return;
            }

文章整理:DIY部落 http://www.diybl.com (本站)   【点击打包该文章】
[1] [2]
如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言

 

最新新闻