2012-10-23 63 views
1

我是初學者在C#中。在我的項目中,用戶通過OpenFileDialog框選擇一個圖像文件。當他/她選擇我正在後面的代碼是這樣的映像文件:將現有文件添加到文件夾

File.Copy(SourceFilePath, DestinationFilePath); 

與上面的代碼的問題是,它是當用戶試圖加入一個現有的圖像文件拋出錯誤。爲了避免這種錯誤,我改變了我的代碼以下之一:

if (File.Exists(DestinationFilePath)) 
{ 
    intCount++; 
    File.Copy(SourceFilePath,TemporaryFilePath); 
    File.Copy(TemporaryFilePath, DestinationFilePath + intCount.ToString()); 
    File.Delete(TemporaryFilePath);         
} 
else 
{ 
    File.Copy(SourceFilePath, DestinationFilePath); 
} 

在上面的代碼的問題是,它是在圖像文件的最後像image.gif1它改變了文件中添加intCount值延期。如何添加計數器到圖像文件路徑?

我認爲我在這裏用來檢查現有文件的方法不是正確的做法。

更新:回答: -

 int intCount = 1; 
     while (File.Exists(Application.StartupPath + DirectoryPath + strPath)) 
     { 
      strPath = Path.GetFileNameWithoutExtension(strPath) + intLarge.ToString() + Path.GetExtension(strPath); 
      intCount++; 
     } 
     intCount = 1; 
+1

您可以使用'Path'類來操作文件名。 – Shaamaan

回答

1

使用

Path.GetFileNameWithoutExtension(string destinationfilename) 

添加int它和擴展程序添加到它,你可以通過

Path.GetExtension(string destinationfilename) 
+0

它顯示錯誤 - 「進程無法訪問文件'filePath',因爲它正在被另一個進程使用。」到你的第一篇文章的方法。 –

+0

使用這個新的帖子片段。 –

1

反而得到作者:

DestinationFilePath + intCount.ToString() 

您可以使用:

intCount.ToString() + DestinationFilePath 

這將其添加到啓動,導致1image.gif

1

最好的方法就是創建一個新的文件路徑。

以下功能將給Image.gif的與1的計數爲image1.gif

private string GetIncrementedFilePath(string orginalFilePath, int count) 
{ 
    var extension = Path.GetExtension(orginalFilePath); 
    var fileName = Path.GetFileNameWithoutExtension(orginalFilePath); 
    var directory = Path.GetDirectoryName(orginalFilePath); 

    var newFullPath = string.Format("{0}\\{1}{2}{3}", directory, fileName, count, extension); 

    return newFullPath; 
} 

請注意,Path.GetExension會給你 '.gif要點' 而不是 'GIF'

// Summary: 
//  Returns the extension of the specified path string. 
// 
// Parameters: 
// path: 
//  The path string from which to get the extension. 
// 
// Returns: 
//  A System.String containing the extension of the specified path (including 
//  the "."), null, or System.String.Empty. If path is null, GetExtension returns 
//  null. If path does not have extension information, GetExtension returns Empty. 
// 
// Exceptions: 
// System.ArgumentException: 
//  path contains one or more of the invalid characters defined in System.IO.Path.GetInvalidPathChars(). 
public static string GetExtension(string path); 

如果你想知道一個文件是否存在,那麼在.NET中有一個內置的函數來實現這個功能。

// Summary: 
//  Determines whether the specified file exists. 
// 
// Parameters: 
// path: 
//  The file to check. 
// 
// Returns: 
//  true if the caller has the required permissions and path contains the name 
//  of an existing file; otherwise, false. This method also returns false if 
//  path is null, an invalid path, or a zero-length string. If the caller does 
//  not have sufficient permissions to read the specified file, no exception 
//  is thrown and the method returns false regardless of the existence of path. 
File.Exists(path); 
+0

謝謝你的迴應,如果用戶再次選擇現有的文件呢?(我正在檢查哪些需要時間) –

+0

我假設這就是爲什麼你有數。如果你增加2你得到image2.gif。還有一個名爲File.Exists的函數,您可以在其中檢查文件是否存在以及是否存在,然後刪除前一個或增加計數。 –

2
private string GetIndexedFilePath(string path, int index) 
{ 
    var directoryName = Path.GetDirectoryName(path); 
    var oldFileName = Path.GetFileNameWithoutExtension(path); 
    var extension = Path.GetExtension(path); 
    var indexedFileName = String.Format("{0}_{1}{2}", oldFileName, index, extension); 
    return Path.Combine(directoryName, indexedFileName); 
} 

順便說一下記住,如果要重命名文件,以類似後「file_2.gif」你仍然可以有在目標目錄中已存在的文件名衝突。

string destinationPath; 
int index = 0; 
do 
{ 
    destinationPath = GetIndexedFilePath(path, ++index); 
} 
while(File.Exists(destinationPath)); 
// Copy file to destinationPath 
1

你可以使用類Path對包含文件或目錄名信息分離的字符串從它的擴展表示文件名System.String情況下進行操作

string Name = Path.GetFileNameWithoutExtension(DestinationFilePath); //Get the file name excluding its extension 
string Extension = Path.GetExtension(DestinationFilePath); //Declare a new string representing the extension of the file 
File.Copy(TemporaryFilePath, DestinationFilePath.Replace(Path.GetFileName(DestinationFilePath), "") + Name + intCount.ToString() + Extension); //Copy from TemporaryFilePath to DestinationFilePath appending a number after the string then the Extension we gathered first 

上面發佈的示例將複製例如TemporaryFilePath\File_Name.(Extension)的文件名稱爲DestinationFilePath\File_Name (intCount).(Extension),其中(intCount)代表數字並且(Extension) re主張擴展文件。因此,文件名的最後一個外觀看起來像下面如果intCount等於1Extension.exe

  • DestinationFilePath\File_Name 1.exe

謝謝,
我希望對您有所幫助:)