【问题标题】:Copy a directory in Windows CE with a C# application使用 C# 应用程序复制 Windows CE 中的目录
【发布时间】:2015-12-09 16:17:45
【问题描述】:

因为我没有找到一种方法来创建可以在 Windows CE 7 (.NET Framework 3.5) 中复制我的目录的批处理脚本,所以我尝试通过创建 C# 应用程序来解决这个问题。

我使用了 VS2008 并将我的“目标框架”设置为 .NET Framework 3.5。我已经从这台 PC 上为 CE 编写了其他应用程序,所以我认为问题出在我的代码上。

我发现一个应用程序已经有了我要找的东西,这是经过一些修改后的样子:

using System;
using System.IO;

class DirectoryCopyExample
{ 
static void Main()
{
    DirectoryCopy(@"\Hard Disk2\BootFastBlink", @"\Hard Disk\TwinCAT\3.1", true);
}

private static void DirectoryCopy(
    string sourceDirName, string destDirName, bool copySubDirs)
{
    DirectoryInfo dir = new DirectoryInfo(sourceDirName);
    DirectoryInfo[] dirs = dir.GetDirectories();

    // If the source directory does not exist, throw an exception. 
    if (!dir.Exists)
    {
        throw new DirectoryNotFoundException(
            "Source directory does not exist or could not be found: "
            + sourceDirName);
    }

    // If the destination directory does not exist, create it. 
    if (!Directory.Exists(destDirName))
    {
        Directory.CreateDirectory(destDirName);
    }


    // Get the file contents of the directory to copy.
    FileInfo[] files = dir.GetFiles();

    foreach (FileInfo file in files)
    {
        // Create the path to the new copy of the file. 
        string temppath = Path.Combine(destDirName, file.Name);

        // Copy the file.
        file.CopyTo(temppath, false);
    }

    // If copySubDirs is true, copy the subdirectories. 
    if (copySubDirs)
    {

        foreach (DirectoryInfo subdir in dirs)
        {
            // Create the subdirectory. 
            string temppath = Path.Combine(destDirName, subdir.Name);

            // Copy the subdirectories.
            DirectoryCopy(subdir.FullName, temppath, copySubDirs);
        }
    }
}

}

当我在 CE 中运行 .exe 文件时,出现错误:

"copyFolderCE.exe DirectoryNotFoundException at System.IO.Directory.InternalGetFileDirectoryNames(String fullPath, Boolean file) at System.IO.DirectoryInfo.GetDirectories"..... 

我认为问题可能是 CE 无法使用这些功能,或者我输入的目录名称有误。

谁能告诉我我做错了什么? 我需要 SDK 吗?

提前致谢。

TLDR:我不想在 Windows CE 7 上将文件从 U 盘复制到硬盘。

【问题讨论】:

  • 首先,换行: DirectoryInfo[] dirs = dir.GetDirectories();尝试/捕获以获取更多详细信息。我猜它不喜欢你的源目录:\Hard Disk2\BootFastBlink

标签: c# .net embedded windows-ce


【解决方案1】:

你的代码很好。我已经在 WEC7 .NET CF 3.5 环境中对其进行了测试,它运行良好:我将一个目录和子目录从 USB 记忆棒复制到 pc 硬盘中。 所以这不是 WEC7 或 .NET 问题。 很可能您的 sourceDirName 是错误的,或者您没有读取权限:当我尝试将系统目录(应用程序数据)复制到 pc 硬盘时,我遇到了与您相同的异常。 或者您没有权限在您的目标目录上写入。

@ 字符可以避免转义 \,因此如果目录名称正确且您有权限,则以下代码很好:

DirectoryCopy(@"\Hard Disk2\BootFastBlink", @"\Hard Disk\TwinCAT\3.1", true);

【讨论】:

  • 谢谢。有机会我会试一试的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
相关资源
最近更新 更多