Como renomear um arquivo no Visual BasicHow to: Rename a File in Visual Basic
Use o método RenameFile
do objeto My.Computer.FileSystem
para renomear um arquivo, fornecendo o local atual, o nome de arquivo e o novo nome de arquivo.Use the RenameFile
method of the My.Computer.FileSystem
object to rename a file by supplying the current location, file name, and the new file name. Esse método não pode ser usado para mover um arquivo. Use o método MoveFile
para mover e renomear o arquivo.This method cannot be used to move a file; use the MoveFile
method to move and rename the file.
Para renomear um arquivoTo rename a file
Use o método
My.Computer.FileSystem.RenameFile
para renomear um arquivo.Use theMy.Computer.FileSystem.RenameFile
method to rename a file. Este exemplo renomeia o arquivo chamadoTest.txt
paraSecondTest.txt
.This example renames the file namedTest.txt
toSecondTest.txt
.' Change "c:\test.txt" to the path and filename for the file that ' you want to rename. My.Computer.FileSystem.RenameFile("C:\Test.txt", "SecondTest.txt")
Este exemplo de código também está disponível como um snippet de código do IntelliSense.This code example is also available as an IntelliSense code snippet. No selecionador de snippet de código, o snippet está localizado em Sistema de Arquivos – Processando Unidades, Pastas e Arquivos.In the code snippet picker, the snippet is located in File system - Processing Drives, Folders, and Files. Para obter mais informações, consulte Snippets de Código.For more information, see Code Snippets.
Programação robustaRobust Programming
As seguintes condições podem causar uma exceção:The following conditions may cause an exception:
O caminho não é válido por um dos seguintes motivos: é uma cadeia de comprimento zero, contém apenas espaços em branco, contém caracteres inválidos ou é um caminho de dispositivo (começa com \\.\) (ArgumentException).The path is not valid for one of the following reasons: it is a zero-length string, it contains only white space, it contains invalid characters, or it is a device path (starts with \\.\) (ArgumentException).
newName
contém informações de caminho (ArgumentException).newName
contains path information (ArgumentException).O caminho não é válido porque é
Nothing
(ArgumentNullException).The path is not valid because it isNothing
(ArgumentNullException).newName
éNothing
ou é uma cadeia de caracteres vazia (ArgumentNullException).newName
isNothing
or an empty string (ArgumentNullException).O arquivo de origem não é válido ou não existe (FileNotFoundException).The source file is not valid or does not exist (FileNotFoundException).
Há um arquivo ou diretório existente com o nome especificado em
newName
(IOException).There is an existing file or directory with the name specified innewName
(IOException).O caminho excede o comprimento máximo definido pelo sistema (PathTooLongException).The path exceeds the system-defined maximum length (PathTooLongException).
Um nome de arquivo ou de diretório no caminho contém dois-pontos (:) ou está em um formato inválido (NotSupportedException).A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
O usuário não possui permissões necessárias para exibir o caminho (SecurityException).The user lacks necessary permissions to view the path (SecurityException).
O usuário não tem a permissão necessária (UnauthorizedAccessException).The user does not have the required permission (UnauthorizedAccessException).
Consulte tambémSee also
- RenameFile
- Como: mover um arquivoHow to: Move a File
- Criar, excluir e mover arquivos e diretóriosCreating, Deleting, and Moving Files and Directories
- Como: criar uma cópia de um arquivo no mesmo diretórioHow to: Create a Copy of a File in the Same Directory
- Como: criar uma cópia de um arquivo em outro diretórioHow to: Create a Copy of a File in a Different Directory