Samuel Pardee Samuel Pardee - 1 year ago 73 Question

Changing an existing file's extension: Visual Studio 2010

After searching for quite sometime now, one cannot just find a simple answer. So here is my question:

I am writing in Visual Basic from Visual Studio 2010.

(Another question that I need answered but is not relevant: Is this language referred to as VB .NET ?; I am new to this language but not to programming as I am a web designer.)

I have a file in this directory: C:\users\Debbie

This file's name and extension are as followed:

What I need to know is: How do I take this file that I have created and quickly change the extension without moving it to another directory or making a copy. I just want my program to find the file and quickly change it to backup01.nbu

.nbu will be the file extensions that my software will be able to open (when I get there).


(P.S.) If anyone is curious as to why I am already writing files it would be because I am jumping into this and trying my best to create simple backup software for my Mother whose original software that she uses frequently on her computer does not have backup capabilities. Keep in mind that I am an aspired programmer familiar with languages such as HTML, CSS, PHP etc. and I am trying to pick this one up by myself as well.

Answer Source

Use the File.Move method:

System.IO.File.Move("C:\users\Debbie\", "C:\users\Debbie\backup01.nbu")

You likely have the file name in a variable already, so Path.ChangeExtension will be handy:

Dim originalFile As String = "C:\users\Debbie\"
Dim newName As String = Path.ChangeExtension(originalFile, "nbu")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download