【问题标题】:The path is not a legal form [closed]路径不是法律形式[关闭]
【发布时间】:2015-08-27 08:31:59
【问题描述】:

当 debug..Button1 是关于 zip 文件时会出现此错误,然后将其从源复制到目标..在删除功能中,它会删除创建时间早于给定时间的文件.. 注意:我是新手,所以如果可能的话,请在你的回答中提供很多细节。

private void button1_Click(object sender, EventArgs e)
{
    Backup._MAIN takeBackUp = new Backup._MAIN();
    try
    {
        takeBackUp.copyFile(textBox1.Text, textBox2.Text);
         MessageBox.Show("done");  //do stuff
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void button2_Click(object sender, EventArgs e)
{
    bool dater;
    string err = "";
    Backup._MAIN formDelete = new Backup._MAIN();
    DateTime dtp = dateTimePicker1.Value;
    dater = formDelete.deleteFiles(textBox3.Text, dtp,out err);
    if (err.Length > 0)
        MessageBox.Show(err);
    else
        MessageBox.Show("Done!");
}

DLL:

public void copyFile(string sSource,string sDestination)
{
    string sourcePath = sSource;
    string targetPath = sDestination;
    Directory.GetFiles(sourcePath,"",SearchOption.AllDirectories);

    string myBackUp="";
    string fileName;

    if (!System.IO.Directory.Exists(targetPath))
    {
        System.IO.Directory.CreateDirectory(targetPath);
    }

    using (ZipFile zip = new ZipFile(targetPath))
    {
        zip.AddDirectory(sourcePath, "");
        zip.Save(myBackUp);
    }

    if (System.IO.Directory.Exists(sourcePath))
    {
        string[] fileList = System.IO.Directory.GetFiles(sourcePath, "*.rar*");

        foreach (string files in fileList)
        {
            // from the path.
            fileName = System.IO.Path.GetFileName(files);
            targetPath = System.IO.Path.Combine(targetPath, fileName);
            System.IO.File.Copy(files, targetPath, true);
        }
    }
}

public bool deleteFiles(string sSource, DateTime dOlder, out string sError)
{
    string path = sSource;
    sError = "";
    try
    {
        string[] fileList = Directory.GetFiles(path);
        foreach ( string fname in fileList )
        {

            if ( File.GetCreationTime(path + fname) <= dOlder )
            {
                File.Delete(path + fname);
            }
        }
        return true;
    }

    catch ( Exception ex )
    {
        sError = ex.ToString();
        return false;
    }
}

【问题讨论】:

  • 它把乳液放在篮子里,或者它又得到软管。你能改写你的第一段,不知道你的意思
  • 你能告诉我们实际的问题是什么吗?您刚刚发布了代码并解释了它的作用..
  • @MickyDuncan 现在我感觉像一些蚕豆
  • 我更新了它...问题在标题中..路径不是合法形式...它们都给出相同的错误
  • 我认为您的源路径不是合法形式 - 您应该重新格式化它以使其正确。如果您想要更有用的评论,请提供您正在使用的源路径。此外,设置断点并单步执行代码。

标签: c# forms path


【解决方案1】:

Directory.GetFiles() 返回完整路径名。

但是,请考虑您的代码:

string[] fileList = Directory.GetFiles(path);

foreach ( string fname in fileList)
{
    if ( File.GetCreationTime(path + fname) <= dOlder) 

您在执行path + fname 时为路径添加前缀,这将导致路径无效,因为fname 已包含该路径。

如果路径包含驱动器号,例如 C:\,那么您最终会得到类似 C:\MyDir\C:\MyDir\MyFile.txt 的路径,这会给您带来您所看到的错误。

您只需要做File.GetCreationTime(fname)File.Delete() 也是如此。

【讨论】:

  • 是的,它成功了!非常感谢。你能找到copyFile函数的问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多