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



因此,我正在尝试将文件从源复制到目标。我正在创建一个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)
    {
    }
  }
}

首先,对于某些干净的代码,您的field命名file至少应该是property,名称应该反映其实际是什么,因此:

string file = "";

to

private string FileFullPath { get; set; }

那么,真正的问题是您将文件的完整路径分配给filefile = openFileDialog1.FileName;,然后将其视为目录string sourceDir = file;,这应该很明显,为什么会失败……如果没有。..您需要走完整的路径,然后获得目录,即:

var sourceDir = Path.GetDirectoryName(FileFullPath);

最新更新