该目录无效。怎么了?将文件从源复制到目标

时间:2019-05-10 01:45:17

标签: c# file

所以我试图将文件从源复制到目标。我正在创建一个Windows窗体,其中有按钮,源和目标。它们用于获取文件,然后获得目标位置。然后使用另一个按钮将该文件复制到目标位置。当我单击目标位置时,显示“目录名称无效”。

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

namespace CopyDirectory
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    string file = "";
    private void button1_Click(object sender, EventArgs e)
    {
        DialogResult result = openFileDialog1.ShowDialog();
        if (result == DialogResult.OK) // Test result.
        {
            //opens the file source & shows it in a label
            file = openFileDialog1.FileName;
            try
            {
                string text = File.ReadAllText(file);
                int size = text.Length;
                string sfile = Path.GetFileName(file);
                lbl_sfile.Text = sfile; // for full location
            }
            catch (IOException)
            {
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK) // Test result.
        {
            //saves the file destination & shows it in a label

            //use file2 string to save file into destination

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                lbl_dfile.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
    private void Caluculate(int i)
    {
        double pow = Math.Pow(i, i);
    }

    private void bttn_savefile_Click(object sender, EventArgs e)
    {
        //collect label text as strings
        string file2 = lbl_sfile.Text.ToString();
        string file3 = lbl_dfile.Text.ToString();

        string sourceDir = file;
        string backupDir = folderBrowserDialog1.SelectedPath;

        Path.Combine(file2, Path.GetFileName(file3));

        string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
        string[] txtList = Directory.GetFiles(sourceDir, "*.txt");

        // Copy text files.
        foreach (string f in txtList)
        {

            // Remove path from the file name.
            string fName = f.Substring(sourceDir.Length + 1);

            try
            {
                // Will not overwrite if the destination file already exists.
                File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
            }

            // Catch exception if the file was already copied.
            catch (IOException copyError)
            {
                Console.WriteLine(copyError.Message);
            }
        }


        // Set the initial value of the ProgressBar.
        progressBar1.Value = 10;

        progressBar1.Maximum = 100000;
        progressBar1.Step = 1;

        for (int j = 0; j < 100000; j++)
        {
            Caluculate(j);
            progressBar1.PerformStep();
        }
    }

    private void progressBar1_Click(object sender, EventArgs e)
    {

    }
  }
}

1 个答案:

答案 0 :(得分:0)

首先,对于一些简洁的代码,您的field命名为file至少应为property,并且名称应反映其实际含义,因此:

string file = "";

private string FileFullPath { get; set; }

然后,真正的问题是您正在将文件的完整路径(包括其名称)分配给filefile = openFileDialog1.FileName;,但随后将其视为目录string sourceDir = file;,这应该很明显关于失败的原因...如果不是...,您需要使用完整路径并仅获取目录,即:

var sourceDir = Path.GetDirectoryName(FileFullPath);