繁体   English   中英

使用C#应用程序在Windows CE中复制目录

[英]Copy a directory in Windows CE with a C# application

因为我没有找到创建可以在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:我不会将文件从USB记忆棒复制到Windows CE 7的硬盘上。

您的代码是好的。 我已经在WEC7 .NET CF 3.5环境中对其进行了测试,并且可以完美运行:我将USB记忆棒中的目录和子目录复制到了PC硬盘中。 因此,这不是WEC7或.NET问题。 您的sourceDirName很可能是错误的,或者您没有对此的读取权限:当我尝试将系统目录(应用程序数据)复制到pc硬盘中时,遇到与您相同的异常。 否则,您无权在目的地目录上进行写操作。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM