2015-04-05 37 views
0

我在Windows窗體應用程序中有幾個問題。到目前爲止,我已設法循環一個文件夾並在文本框中顯示reuslts。一旦這些信息返回,用戶可以檢查結果並使用以下方法刪除錯誤文件;刪除文件,從另一個文件夾複製相同的命名文件C#

private void button2_Click(object sender, EventArgs e) 
{ 
    foreach (string file in Directory.GetFiles(@"\\" + textBox1.Text + @"\\d$\\NSB\\Coalition\\EDU", "*.err").Where(item => item.EndsWith(".err"))) 
    { 
     File.Delete(file); 
    } 
} 

我想現在要做的,錯誤的文件已被刪除後,相同的文件從備份文件夾複製(文件名中的唯一區別是文件擴展),我使用了一個單獨的按鈕這個動作。任何幫助在這最後一步將不勝感激。

+0

爲什麼不移動,並覆蓋文件..這個研究材料的大量太 – Sayse 2015-04-05 14:40:11

+0

感謝喜期待你的答覆。錯誤文件並不總是需要在文件夾內進行替換。有時候他們只需要被刪除。 – TAR 2015-04-05 15:19:51

回答

1

使用Path類來獲得文件名沒有擴展,結合路徑多,作爲一個例子:

StringCollection filesToBeReplaced = new StringCollection(); 

private void button2_Click(object sender, EventArgs e) 
{ 
foreach (string file in Directory.GetFiles(@"\\" + textBox1.Text +  @"\\d$\\NSB\\Coalition\\EDU", "*.err").Where(item => item.EndsWith(".err"))) 
{ 
    //Now you have file names without extension   
    filesToBeReplaced.Add(Path.GetFileNameWithoutExtension (file)); 
     File.Delete(file); 
    } 
} 
private void CopyGoodFilesFromSource() 
{ 
    foreach(string fileName in filesToBeReplaced) 
    { 
    string sourceFilePath = Path.Combine("YOUR FOLDER FOR GOOD FILES", 
Path.ChangeExtension(fileName,"Your Extension")) ; 
     string destinationPath = Path.Combine("Destination Folder", 
Path.ChangeExtension(fileName, "Your Extension in destination folder"); 

    File.Copy(sourceFilePath , destinationPath, true); 
    } 
} 
+0

完美地工作,謝謝! – TAR 2015-04-05 17:19:18

+0

很高興我能夠幫助:) – Ronny 2015-04-05 17:59:59

相關問題