簡體   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