用C#打开文件对话框的方法和简单使用的程序

上面是我创建的Winform窗体,拉控件就这样,groupbox显示文件的相关信息

代码如下:

 
        //实例化文件对话框的类对象
        OpenFileDialog ofg = new OpenFileDialog();
        //存放源文件路径,一个存放目的文件路径
        string filepath;
        string filepath1;
        //获取文件对象
        FileInfo file = null;
        private void button1_Click(object sender, EventArgs e)
        {           
            //调用对象的函数
            if (ofg.ShowDialog() == DialogResult.OK) {
                //获取文件的路径
                filepath = ofg.FileName;
                this.txb_Path1.Text = filepath;
                //创建文件对象
                file = new FileInfo(filepath);
                //获取文件名
                this.txtFileName.Text = file.Name;
                //获取文件完整路径
                this.txtFilePathAll.Text = file.FullName;
                //获取文件所在的根目录
                this.txtFileRoot.Text = file.DirectoryName;
                //获取文件的创建时间、访问时间和修改时间
                this.txtCreatetime.Text = file.CreationTime.ToString();
                this.txtVisttime.Text = file.LastAccessTime.ToString();
                this.txtUpdatetime.Text = file.LastWriteTime.ToString();
                //文件长度和扩展名
                this.txtFileSize.Text = file.Length.ToString();
                this.txtExeName.Text = file.Extension.ToString();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //获取文件目录
            FolderBrowserDialog fog = new FolderBrowserDialog();
            if (fog.ShowDialog() == DialogResult.OK)
            {
                //获取存放目录(文件夹)
                filepath1 = fog.SelectedPath;
                this.txb_Path2.Text = filepath1;
            }
        }
   /// <summary>
        /// 进行复制操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            filepath1 = filepath1+@"\" + file.Name;
            File.Copy(filepath,filepath1);
            MessageBox.Show("复制成功!");

        }
        /// <summary>
        /// 进行剪切(移动)操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            filepath1 = filepath1 + @"\" + file.Name;
            File.Move(filepath,filepath1);
            MessageBox.Show("剪切成功!");
        }

运行效果如下:

 

posted @ 2015-09-04 11:28  小小邪  阅读(5611)  评论(0编辑  收藏  举报