2012-04-02 5 views
4

これにどう対処するかは完全には分かりません。私はちょっと調べたことがあるが、私は不足している。作業中のネットワークドライブに接続しようとしていて、最新のフォルダをコピーする(プロジェクトの更新)私にとっては、ディレクトリは\として始まりますが、文字列変数に追加すると接続されずに表示されませんそれをチェックしようとする。これにはプロセスがありますか?ネットワークドライブからの接続/コピーC#

これは私が持っているものです。それはどこかで間違っていなければならない。

string updir = @"\\NetworkDrive\updates\xxxxx"; 

public void CopyAll(DirectoryInfo source, DirectoryInfo target) 
    { 

     try 
     { 
      //check if the target directory exists 
      if (Directory.Exists(target.FullName) == false) 
      { 
       Directory.CreateDirectory(target.FullName); 
      } 

      //copy all the files into the new directory 

      foreach (FileInfo fi in source.GetFiles()) 
      { 
       fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true); 
      } 


      //copy all the sub directories using recursion 

      foreach (DirectoryInfo diSourceDir in source.GetDirectories()) 
      { 
       DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name); 
       CopyAll(diSourceDir, nextTargetDir); 
      } 
      //success here 
      copyall = true;  
     } 

     catch (IOException ie) 
     { 
      //handle it here 
      copyall = false; 
     } 
    } 

私はこれをコピーに使用しています。それはうまくいく。

DateTime lastHigh = new DateTime(1900, 1, 1); 
     string highDir; 
     foreach (string subdir in Directory.GetDirectories(updir)) 
     { 
      DirectoryInfo fi1 = new DirectoryInfo(subdir); 
      DateTime created = fi1.LastWriteTime; 

      if (created > lastHigh) 
      { 
       highDir = subdir; 
       lastHigh = created; 
      } 
     } 

と最新のフォルダを見つけるためのものです。

+1

は、あなたが使用したコードを示してもらえますか? – Msonic

+0

iveは私のドライブをドライブにアクセスしようとしていたもので更新しました。 –

+0

これはあなたのすべてのコードにはできません。コピー方法全体を投稿してください。どこかに '.Copy()'があるはずです。 – Msonic

答えて

4

あなたは(ネットワーク共有のアクセス権を指定する)は、このような何かを試すことができます。

string updir = @"\\NetworkDrive\updates\somefile"; 

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); 
WindowsIdentity identity = new WindowsIdentity(username, password); 
WindowsImpersonationContext context = identity.Impersonate(); 

File.Copy(updir, @"C:\somefile", true); 
+0

これは私が必要としたものの基礎となった。ありがとうございます –

+2

私はこれを動作させることができません。まず、第2のパラメータは、パスワードではなく「タイプ」として定義されます。これの2番目は、UnauthorizedAccessExceptionをスローするWindowsIdentity.GetCurrent()と同じである必要があります。実際にどうやってこれを稼働させましたか? – Wouter

関連する問題