2

要件:

マシン A にあるフォルダ/ディレクトリとその内容をマシン B に正常にコピーする必要があります。

コピーを開始する前に、私の要件について次の点を考慮する必要があります。

  1. コピー先のマシン、コピー先のフォルダに、コピー元のフォルダまたはディレクトリからコピーする必要があるユーザーのアクセス許可があるかどうか。

  2. 宛先ディレクトリは非表示または共有にしないでください。既に存在する場合は空にする必要があります。

  3. 宛先マシンは、それに応じて同じものを処理するために、アクセスのための資格情報を期待しています

これを達成する方法は?

以下のコードでは達成できません。

using System;
using System.IO;

class DirectoryCopyExample
{
 static void Main()
 {
    DirectoryCopy(".", @".\temp", 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);
        }
    }
 }
}
4

4 に答える 4

2

このすべての情報は;にDirectoryInfoあります。DirectoryInfo dirInfo = new DirectoryInfo(path)

于 2009-12-30T09:41:05.247 に答える
2

あなたのコード スニペットは、実際には 2 台のマシン間でファイルをコピーするのではなく、同じマシン内でコピーします。Main メソッドから明らかです。

マシン間で転送し、それも共有されていない場所に転送したい場合は、おそらくソケットを確認する必要があります。

適切な権限が設定された共有の場所であれば、File.copy が役に立ちます。

于 2009-12-30T09:54:42.317 に答える
1

ファイル送信については、ここであなた自身の質問を見ることができます: Machine to Machine File transfer AND some short script here: http://www.eggheadcafe.com/community/aspnet/2/10076226/file-transfer-from-one-ma. aspx

それよりも、FileInfo と DirectoryInfo を使用して、属性を取得します: http://msdn.microsoft.com/en-us/library/system.io.directoryinfo%28VS.71%29.aspx

リモート フォルダーへの接続は、DirectorySecurity を使用して行うことができます: http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.directorysecurity.aspx

楽しみ!

于 2009-12-30T09:51:32.230 に答える
0

System.Security.AccessControl.DirectorySecurityクラスを確認してください。 DirectorySecurity オブジェクトは次の呼び出しで取得できます。 System.IO.Directory.GetAccessControl("DIR_NAME") これでリモート マシンのディレクトリに関する情報を取得できるかどうかわかりません。

于 2009-12-30T09:42:35.810 に答える