,當我們創建一個與現有文件具有相同名稱的新文件時,不會提示用戶輸出文件已存在的消息,但它創建了(1)在文件名末尾...在windows環境和幾個應用程序中創建'新文件Windows'行爲
name.doc
將
name(1).doc
我試圖創建在C#中相同的行爲一個新的文件,我的問題是, ...我可以減少所有這些代碼嗎?
FileInfo finfo = new FileInfo(fullOutputPath);
if (finfo.Exists)
{
int iFile = 0;
bool exitCreatingFile = false;
while (!exitCreatingFile)
{
iFile++;
if (fullOutputPath.Contains("(" + (iFile - 1) + ")."))
fullOutputPath = fullOutputPath.Replace(
"(" + (iFile - 1) + ").",
"(" + iFile + ")."); // (1) --> (2)
else
fullOutputPath = fullOutputPath.Replace(
Path.GetFileNameWithoutExtension(finfo.Name),
Path.GetFileNameWithoutExtension(finfo.Name) + "(" + iFile + ")"); // name.doc --> name(1).doc
finfo = new FileInfo(fullOutputPath);
if (!finfo.Exists)
exitCreatingFile = true;
}
}
多清潔:)謝謝 – balexandre 2009-06-02 12:00:35