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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
as per http://msdn.microsoft.com/en-us/library/2ce15by3.aspx

you can use the My.Computer.FileSystem.RenameFile method
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
É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
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

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
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

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
Éric MoreauSenior .Net ConsultantCommented:
because you are doing a copy. use Name instead.
käµfm³d 👽Commented:
You can use Move as well.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
File.Name does not exist. Move works without True in the code.

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

 My.Computer.FileSystem.RenameFile(Application.StartupPath & "\" & "LinkFiles\LinkNSN.xml", Application.StartupPath & "\" & "\LinkFiles\LinkNSN" & Trim(LoginForm1.Ctry.Text) & ".xml")
vcharlesAuthor Commented:
Thank you  for the clarification.
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.
É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")
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.