2012-05-28 11 views
5

VB.NETのファイルの名前を変更する方法を理解しています。しかし、私はファイルの名前を変更することが可能かどうか、ファイルが存在する場合はそれを名前を変更し、ファイル名に+1を追加するのだろうか?VB.NETでファイルの名前を変更する方法

コードを実行した場合。

「が初めて実行

My.Computer.FileSystem.RenameFile("c:\test\test.txt", "c:\test\NewName.txt") 

」再びそれを実行して、それは 『C:\テスト\のNewName1.txt』でなければなりませんので、それは、意志が既に存在するファイルとして+1を追加する必要があります

My.Computer.FileSystem.RenameFile("c:\test\test.txt", "c:\test\NewName.txt") 

更新

私が決めたのではなく名前を変更

と+1、それは私が行ったように苦労誰のためにこれだけの日付スタンプ、それを、する方が良いだろう

My.Computer.FileSystem.RenameFile("c:\test\test.txt", "Test" & Format(Date.Now, "ddMMyy") & ".txt") 

答えて

8

これに独自のロジックを記述する必要があります。

Fileクラスには、ファイルを処理するための多くの便利な方法があります。

If File.Exists(filePath) Then 
    ' Give a new name 
Else 
    ' Use existing name 
End If 

Pathクラスには、ファイルパスを処理する方法が多数あります。

Path.GetFileNameWithoutExtension(filePath) 
6
If System.IO.File.Exists("c:\test\NewName.txt") Then 
    ' add +1 or loop exists with increment on the end until file doesn't exist 
End If 
+0

申し訳ありません。コーディングを追加できませんので、最初の投稿をご覧ください。 – JackSparrow

2

あなたはそうあなたがArgumentExceptionを取得するだけで、ここで新しいファイル名を言及し、newFileNameパラメータに完全なファイルパスを言及する必要はありません。

Dim filePath As String = "C:\fingerprint1" 

If File.Exists(filePath) Then 

    Dim strNewFileName As String = "Fingerprint221" 

    My.Computer.FileSystem.RenameFile(filePath, strNewFileName) 

End If 
関連する問題