2012-10-03 27 views
1

有時,在我的應用程序中,我想創建一個已經存在的文件的副本,但我不想測試該文件是否已經存在,我只想要即創建該文件的副本,如Windows 7。創建像Windows7一樣的文件的副本通常做

例如:文件tips.txt。當我的應用程序複製它時,將創建另一個文件,名爲tips - copy.txt。之後,如有必要,提供「副本副本」提示 - 副本 - copy.txt

在這種情況下我能做些什麼嗎?

Obs:在這個應用程序中,我使用.NET 3.5和WPF。

Obs2:我提出這個問題是因爲我認爲.NET中已經存在類似的東西。

+0

爲什麼你不想測試這個文件是否已經存在,Windows在給你提示的時候確實存在,那你爲什麼不去做Windows? –

+0

嘗試在文件上使用ctrl-c ctrl-v。我想創建相同的功能(因爲我認爲.NET中已經存在類似的東西) –

回答

6

你應該提取文件名,然後擴展做一個簡單的File.Copy用新格式的名稱

string fileName = "tips.txt"; 
    string file = Path.GetFileNameWithoutExtension(fileName); 
    string ext = Path.GetExtension(fileName); 
    File.Copy(fileName, string.Format("{0} - Copy{1}", file, ext); 

事情得到了一點更復雜的,如果你有一個FULLPATH從

string fileName = "C:\\test\\tips.txt"; 
    string path = Path.GetDirectoryName(fileName); 
    string file = Path.GetFileNameWithoutExtension(fileName); 
    string ext = Path.GetExtension(fileName); 
    File.Copy(fileName, Path.Combine(path, string.Format("{0} - Copy{1}", file, ext)); 

複製,但如果你真的想模仿Windows資源管理器的行爲,我們應該做的:

string fileName = "C:\\test\\tips.txt"; 
string path = Path.GetDirectoryName(fileName); 
string file = Path.GetFileNameWithoutExtension(fileName); 
string ext = Path.GetExtension(fileName); 
if(file.EndsWith(" - Copy")) file = file.Remove(0, file.Length - 7); 
string destFile = Path.Combine(path, string.Format("{0} - Copy{1}", file, ext)); 
int num = 2; 
while(File.Exists(destFile)) 
{ 
    destFile = Path.Combine(path, string.Format("{0} - Copy ({1}){2}", file, num, ext)); 
    num++; 
} 
File.Copy(fileName, destFile); 

如果Windows資源管理器的副本與「結尾的文件 - 複製「,它將累進數字添加到目標文件,而不是另一個」 - 複製「。
您還應該考慮字符串「複製」是本地化的,因此它在操作系統的非英文版本中發生變化。

+0

好吧,現在我已經測試了整個東西,它似乎是正確的 – Steve

+0

+1我相信你的第三塊代碼正是OP正在尋找的東西。但請檢查您使用的大小寫(複製vs複製)。 –

+0

是的,這是針對我的情況的最佳解決方案。非常感謝@Steve。 –

1
+0

它會生成一個異常,表明該文件已經存在。 –

+0

@FelipeVolpatto - 抓住異常並處理它。如果你不想檢查文件是否已經存在,那麼這是唯一的解決方案。 –

+1

@Ramhound不,它不創建文件。 –

3

不知道這是你的意思到底是什麼,但:

string fileName = "tips.txt"; 
File.Copy(fileName, string.format("{0} - copy", fileName); 

或者:

File.Copy(fileName, string.format("{0} - copy{1}", 
    Path.GetFileNameWithoutExtension(fileName), 
    Path.GetExtension(fileName)); 
+0

是的。但是,框架中是否存在自動執行此操作的內容?我不確定,但DOS上的ROBOCOPY不會執行某些文件? –

+0

@Steve - 查看最新編輯 –

+0

@FelipeVolpatto - 「自動」是什麼意思? –

0

好吧,不測試。只需複製文件,不要處理異常

try 
{ 
File.Copy("",""); 
} 
finally 
{ 
} 
0

這樣的事情?我認爲這樣做有很多簡單的方法。

string destination = ""; 
var fileInfo = new FileInfo(@"c:\temp\tips.txt"); 
var ext = fileInfo.Extension; 
var filename = fileInfo.Name.Remove(fileInfo.Name.Length - 4); 
File.Copy(fileInfo.FullName, destination + filename + " - Copy" + ext); 
1

什麼:

public static void CopyFileLikeWin7(string pathIn,string fileName, string pathOut) 
    { 
     string potentialFileName = Path.Combine(pathOut,fileName); 
     if(File.Exists(potentialFileName)) 
     { 
      CopyFileLikeWin7(pathIn, Path.GetFileNameWithoutExtension(fileName) + "-copy" + Path.GetExtension(fileName), pathOut); 
     } 
     else 
     { 
      File.Copy(pathIn,potentialFileName); 
     } 
    } 
1

如何

string fileName = "tips.txt"; 
string filenamewithoutext = Path.GetFileNameWithoutExtension(fileName); 
string ext = Path.GetExtension(fileName); 
File.Copy(fileName, string.format("{0} - Copy{1}", filenamewithoutext, ext); 
3

除了在System.IO名稱空間中建議使用類的其他答案(如果要獲得與Windows複製對話框完全相同的語義),您可以使用IFileOperation COM對象。這是一個managed wrapper它。