c# - "Could not find a part of the path"错误信息

标签 c# file-io

我正在用 C# 编程,想从闪存盘复制一个文件夹和子文件夹以启动。

这是我的代码:

private void copyBat()
{
    try
    {
        string source_dir = "E:\\Debug\\VipBat";
        string destination_dir = "C:\\Users\\pc\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";

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

        // Create subdirectory structure in destination    
        foreach (string dir in Directory.GetDirectories(source_dir, "*", System.IO.SearchOption.AllDirectories))
        {
            Directory.CreateDirectory(destination_dir + dir.Substring(source_dir.Length));          
        }

        foreach (string file_name in Directory.GetFiles(source_dir, "*.*", System.IO.SearchOption.AllDirectories))
        {
            File.Copy(file_name, destination_dir + file_name.Substring(source_dir.Length), true);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message, "HATA", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}

我得到一个错误:

Could not find a part of the path E:\Debug\VipBat

最佳答案

您尝试访问的路径不存在。

string source_dir = "E:\\Debug\\VipBat\\{0}";

我确定这不是正确的路径。 Debug文件夹直接在E:驱动器在我看来是错误的。我想一定有项目名称文件夹目录存在。

第二件事;什么是 {0}在你的字符串中。我确定它是一个参数占位符,因为文件夹名称不能包含 {0}这样的名字。所以你需要使用 String.Format()替换实际值。

string source_dir = String.Format("E:\\Debug\\VipBat\\{0}",variableName);

但首先检查您尝试访问的路径是否存在。

关于c# - "Could not find a part of the path"错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21796687/

相关文章:

c# - 通过循环旋转将值从 AAA 递增到 ZZZ

我可以将同一个文件指针分配给第二个文件吗?

java - 在 Java 中,如何使用 posix 通配符语法从多个文件中读取数据?

c# - 绕过 TextBox 的字符限制?

c# - 使用 Chello 在 Trello 上创建卡片

c# - Entity Framework - 带排序依据和分组依据的 Linq 查询

c# - Selenium 2 WebDriver 未按预期评估更新的 DOM

EOF 可以与\n 在同一行吗

java - 需要 int 但有问题并发现零字节

c++ - 如何从数据库中读取 CLongBinary 字段并将其写入 CFile 对象?