Help with renaming an xml file using VB.NET

Hi,

How do you remame an xml file located in my application's foler. For example I would like to change NSN.xml to BELNSN.xml.

Thanks,

Victor
vcharlesAsked:
Who is Participating?
 
käµfm³d 👽Commented:
You can use Move as well.
0
 
Éric MoreauSenior .Net ConsultantCommented:
as per http://msdn.microsoft.com/en-us/library/2ce15by3.aspx

you can use the My.Computer.FileSystem.RenameFile method
0
 
vcharlesAuthor Commented:
Hi,

I tried to follow the approach suggested but the syntax is incorrect, any ideas what is the proper syntax?

 My.Computer.FileSystem.RenameFile((Application.StartupPath + "\LinkFiles\LinkNSN" & Trim(NSN.Text) & ".xml")", (Application.StartupPath + "\LinkFiles\LinkNSN" & Trim(NSN.Text) & ".xml"))

Thanks,

Victor
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Éric MoreauSenior .Net ConsultantCommented:
you rename for the same filename? the first parameter is the current name. the second is the new name. I read twice the same value
0
 
angus_young_acdcCommented:
Why note just use File.Copy()?  With the True parameter it will overwrite the original.

File.Copy(Application.StartUpPath & "\\" & "NMN.xml", Application.StartUpPath & "\\" & "BELNSN.xml", True)

Open in new window


You can also use File.Move, but that won't give the option to overwrite so if something else creates it then you will get an exception
0
 
käµfm³d 👽Commented:
I tried to follow the approach suggested but the syntax is incorrect, any ideas what is the proper syntax?
In addition to the comments you've received above, I would suggest employing a bit of strategic Path.Combine and Path.ChangeExtension to alleviate some of the readability issues you have in that bit of code. For example:

Imports System.IO

...

Dim sourceFilename As String = Path.Combine(Application.StartupPath, "\LinkFiles\LinkNSN")
Dim destinFilename As String = Path.Combine(Application.StartupPath, "\LinkFiles\LinkNSN")

sourceFilename = Path.Combine(sourceFilename, Trim(NSN.Text))
sourceFilename = Path.ChangeExtension(sourceFilename, ".xml")

destinFilename= Path.Combine(destinFilename, Trim(NSN.Text))
destinFilename= Path.ChangeExtension(destinFilename, ".xml")

My.Computer.FileSystem.RenameFile(sourceFilename, destinFilename)

Open in new window

0
 
vcharlesAuthor Commented:
Hi,

The code below worked but the original xml file is not deleted.

  File.Copy(Application.StartupPath & "\" & "LinkFiles\LinkNSN.xml", Application.StartupPath & "\" & "\LinkFiles\LinkNSN" & Trim(LoginForm1.Ctry.Text) & ".xml", True)


Any ideas why the original file(LinkNSN.xml) is not deleted?

Thanks,

Victor
0
 
Éric MoreauSenior .Net ConsultantCommented:
because you are doing a copy. use Name instead.
0
 
vcharlesAuthor Commented:
File.Name does not exist. Move works without True in the code.

Thanks.
0
 
vcharlesAuthor Commented:
Thank You.
0
 
Éric MoreauSenior .Net ConsultantCommented:
I meant:

 My.Computer.FileSystem.RenameFile(Application.StartupPath & "\" & "LinkFiles\LinkNSN.xml", Application.StartupPath & "\" & "\LinkFiles\LinkNSN" & Trim(LoginForm1.Ctry.Text) & ".xml")
0
 
vcharlesAuthor Commented:
Thank you  for the clarification.
0
 
vcharlesAuthor Commented:
I tried your latest approach but getting the following error:

Argument 'newName' must be a name, and not a relative or absolute path: 'C:\Users\victor\Documents\Visual Studio 2010\Projects\TEST_XML_New\TESTNSN_XML\bin\Debug\\LinkFiles\LinkNSNBEL.xml'.
Parameter name: newName

How do i solve this error?

Thanks,

C.
0
 
Éric MoreauSenior .Net ConsultantCommented:
the example on http://msdn.microsoft.com/en-us/library/2ce15by3.aspx shows:

My.Computer.FileSystem.RenameFile(Application.StartupPath & "\" & "LinkFiles\LinkNSN.xml", "LinkNSN" & Trim(LoginForm1.Ctry.Text) & ".xml")
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.