C# 目录无效。出什么事了?将文件从源复制到目标
因此,我试图将文件从源复制到目标。我正在创建一个windows窗体,其中有按钮、源和目标。它们被用来获取一个文件,然后获取一个设计。然后使用另一个按钮将该文件复制到目标。当我单击目标时,我得到“目录名无效”C# 目录无效。出什么事了?将文件从源复制到目标,c#,file,C#,File,因此,我试图将文件从源复制到目标。我正在创建一个windows窗体,其中有按钮、源和目标。它们被用来获取一个文件,然后获取一个设计。然后使用另一个按钮将该文件复制到目标。当我单击目标时,我得到“目录名无效” using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using Syste
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)
{
}
}
}
首先,对于一些干净的代码,名为file的字段至少应该是一个属性,并且名称应该反映它的实际情况,因此:
string file = "";
到
那么真正的问题是您正在分配文件的完整路径,包括文件名:file=openFileDialog1.FileName;但随后将其视为目录字符串sourceDir=file;这应该是显而易见的,为什么会失败。。。如果不是。。。您需要获取完整路径,只需获取目录即:
var sourceDir = Path.GetDirectoryName(FileFullPath);
哪一行会抛出错误?发生时,该行上变量的运行时值是多少?我猜目标目录无效。由于我们不知道发生错误时变量包含什么,这可能是您所能期望的最好结果。@David string[]txtList=Directory.getfilesourcedir,*.txt@KenWhite字符串[]txtList=Directory.getfilesourcedir,*.txt;这并没有告诉我们在赋值之后txtList的值是多少。这将是一个很好的机会,让您学习如何使用调试器在代码运行时逐步执行代码,这样您就可以确切地看到它在做什么,并在代码执行时检查变量的内容。这将修复此问题,但文件不会保存在目录中。
var sourceDir = Path.GetDirectoryName(FileFullPath);