简体   繁体   English

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

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

Because I didn't find a way of creating a batch-script that could copy my directory in Windows CE 7 (.NET Framework 3.5), I've tried to solve this by creating a C# application instead. 因为我没有找到创建可以在Windows CE 7(.NET Framework 3.5)中复制目录的批处理脚本的方法,所以我尝试通过创建C#应用程序来解决此问题。

I've used VS2008 and set my "Target framework" to .NET Framework 3.5. 我使用VS2008,并将“目标框架”设置为.NET Framework 3.5。 I've written other applications for CE from this PC so I think the problem is my code. 我已经从这台PC上为CE编写了其他应用程序,所以我认为问题出在我的代码上。

I found an application that already had what I was looking for, this is how it looks after some modification: 我发现一个应用程序已经有了我想要的东西,这是经过一些修改后的样子:

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);
        }
    }
}

} }

When I run the .exe file in CE, I get an error: 当我在CE中运行.exe文件时,出现错误:

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

I think the problem might be that the functions aren't available for CE, or that I'm entering the directory names wrong. 我认为问题可能是这些功能不适用于CE,或者我输入的目录名称错误。

Anyone here who could tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗? Do I need an SDK? 我需要SDK吗?

Thanks in advance. 提前致谢。

TLDR: I wan't to copy files from a USB stick to the hard disk on Windows CE 7. TLDR:我不会将文件从USB记忆棒复制到Windows CE 7的硬盘上。

Your code is good. 您的代码是好的。 I've tested it in a WEC7 .NET CF 3.5 environment and it works perfectly: I copied a directory and subdirectories from a USB stick into the pc hard disk. 我已经在WEC7 .NET CF 3.5环境中对其进行了测试,并且可以完美运行:我将USB记忆棒中的目录和子目录复制到了PC硬盘中。 So this is not a WEC7 or .NET problem. 因此,这不是WEC7或.NET问题。 Most probably your sourceDirName is wrong or you do not have read permission on it: I get the same exception as you when I tried to copy a system directory (Application Data) into the pc hard disk. 您的sourceDirName很可能是错误的,或者您没有对此的读取权限:当我尝试将系统目录(应用程序数据)复制到pc硬盘中时,遇到与您相同的异常。 Or you do not have permission to write on your destinatio dir. 否则,您无权在目的地目录上进行写操作。

The @ character is good to avoid escaping the \\ and so the following code is good if dir names are correct and you have permissions: 使用@字符可以避免转义\\,因此,如果目录名正确且您具有权限,则以下代码将很有效:

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

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

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