2012-04-26 82 views
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; 
    } 
} 
+0

它有更多的,但我把什么必要了,我需要它对于我工作的工作而言,我需要的是快而不迟。 – shred1894 2012-04-26 17:49:57

+0

你的意思是'Path.GetDirectoryName(filename)'? – mellamokb 2012-04-26 17:54:54

+0

我需要整个路径,而不仅仅是文件所在的文件夹。 – shred1894 2012-04-26 17:57:50

回答

1

Path.GetFullPath作品,也可以考虑使用FileInfo,因为它附带了许多文件辅助方法。

我会使用类似的方法(可能会使用更多的错误处理(try ...渔获量),但它是一个良好的开端

编辑我注意到,你过滤掉了扩展,但要求他们,更新代码允许该

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); 
+0

这些文件在服务器上,它们将被放在另一台服务器上,我想埃里克告诉我的工作原理(除了以外的任何其他原因权限),它有一个权限问题,我将不得不整理出来。没有什么比真正需要错误处理。 – shred1894 2012-04-26 18:30:43

+0

@ shred1894 Path.GetDirectoryName(文件)不比我提供的代码更好。使用UNC路径,即'\\ servername \ ...'与代码无关,此代码也可以正常工作。在您选择的任何可行解决方案中,即使不考虑本地或远程文件,您的进程也需要适当的读取和写入权限。 – payo 2012-04-26 18:34:31

+0

@ shred1894请注意,我使用'FileInfo',因为它有很多很棒的帮助器方法来非常容易地获取路径和名称以及扩展名。 – payo 2012-04-26 18:35:25