C#文件复制功能

目的是将用户自定义文件复制到指定文件夹并且能查看该文件,下面是个人做的源码:

sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string result, path;
        string S, S1;

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            if (folder.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = folder.SelectedPath;
                path = System.IO.Path.Combine(folder.SelectedPath, result);//目标路径
                textBox3.Text = path;
                S1 = folder.SelectedPath;//文件夹地址赋值
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            if (file.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = file.FileName;
                S = file.FileName;//文件地址赋值
                result = Path.GetFileName(file.FileName); //文件名


            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (File.Exists(path))
            {
                MessageBox.Show("文件已存在");
            }
            else
            {
                // FileInfo xx = new FileInfo(S);//移动
                // xx.MoveTo(path);    
               // string fileName = System.IO.Path.GetFileName(S);//返回指定路径字符串的文件名和扩展名,和Path.GetFileName(file.FileName); 功能一样
                System.IO.File.Copy(S, path, true);

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
           // System.Diagnostics.Process.Start("S1", path);

            OpenFileDialog A =new  OpenFileDialog();
           // A.FileName = S1;
            A.ShowDialog();
        }
    }
}


你可能感兴趣的:(文件复制)