2012-04-26 59 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