0

Directory.GetFilesコピーされるファイルを見つけるために使用しています。コピーを使用できるようにファイルのパスを見つける必要がありますが、パスを見つける方法がわかりません。ファイルを正常に反復処理しますが、ファイルのソース パスが必要なため、ファイルをコピーまたは移動できません。

これは私が持っているものです:

string[] files = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories);

System.Console.WriteLine("Files Found");

// Display all the files.
foreach (string file in files)
{
  string extension = Path.GetExtension(file);
  string thenameofdoom = Path.GetFileNameWithoutExtension(file);
  string filename = Path.GetFileName(file);

  bool b = false;
  string newlocation = (@"\\TEST12CVG\Public\Posts\Temporaryjunk\");

  if (extension == ".pst" || 
    extension == ".tec" || 
    extension == ".pas" || 
    extension == ".snc" || 
    extension == ".cst")
  {
    b = true;
  }

  if (thenameofdoom == "Plasma" || 
    thenameofdoom == "Oxygas" || 
    thenameofdoom == "plasma" || 
    thenameofdoom == "oxygas" || 
    thenameofdoom == "Oxyfuel" || 
    thenameofdoom == "oxyfuel")
  {
    b = false;
  }

  if (b == true)
  {
    File.Copy(file, newlocation + thenameofdoom);
    System.Console.WriteLine("Success: " + filename);
    b = false;
  }
}
4

1 に答える 1

1

Path.GetFullPathFileInfo動作しますが、多くのファイル ヘルパー メソッドが付属しているため、使用することも検討してください。

私はこれに似た方法を使用します(より多くのエラー処理を使用できます(キャッチを試してください...)が、それは良いスタートです

編集拡張機能を除外していることに気付きましたが、拡張機能が必要なため、コードを更新するとそれが可能になります

class BackupOptions
{
  public IEnumerable<string> ExtensionsToAllow { get; set; }
  public IEnumerable<string> ExtensionsToIgnore { get; set; }
  public IEnumerable<string> NamesToIgnore { get; set; }
  public bool CaseInsensitive { get; set; }

  public BackupOptions()
  {
    ExtensionsToAllow = new string[] { };
    ExtensionsToIgnore = new string[] { };
    NamesToIgnore = new string[] { };
  }
}

static void Backup(string sourcePath, string destinationPath, BackupOptions options = null)
{

  if (options == null)
    optionns = new BackupOptions();

  string[] files = Directory.GetFiles(sourcePath, ".", SearchOption.AllDirectories);
  StringComparison comp = options.CaseInsensitive ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture;

  foreach (var file in files)
  {
    FileInfo info = new FileInfo(file);

    if (options.ExtensionsToAllow.Count() > 0 &&
      !options.ExtensionsToAllow.Any(allow => info.Extension.Equals(allow, comp)))
      continue;

    if (options.ExtensionsToIgnore.Any(ignore => info.Extension.Equals(ignore, comp)))
        continue;

    if (options.NamesToIgnore.Any(ignore => info.Name.Equals(ignore, comp)))
      continue;

    try
    {
      File.Copy(info.FullName, destinationPath + "\\" + info.Name);
    }
    catch (Exception ex)
    {
      // report/handle error
    }
  }
}

次のような呼び出しで:

var options = new BackupOptions
{
  ExtensionsToAllow = new string[] { ".pst", ".tec", ".pas", ".snc", ".cst" },
  NamesToIgnore = new string[] { "Plasma", "Oxygas", "Oxyfuel" },
  CaseInsensitive = true
};

Backup("D:\\temp", "D:\\backup", options);
于 2012-04-26T18:12:17.157 に答える