Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

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
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

as per http://msdn.microsoft.com/en-us/library/2ce15by3.aspx

you can use the My.Computer.FileSystem.RenameFile method
Avatar of Victor  Charles

ASKER

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
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
SOLUTION
Avatar of angus_young_acdc
angus_young_acdc
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
because you are doing a copy. use Name instead.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
File.Name does not exist. Move works without True in the code.

Thanks.
Thank You.
I meant:

 My.Computer.FileSystem.RenameFile(Application.StartupPath & "\" & "LinkFiles\LinkNSN.xml", Application.StartupPath & "\" & "\LinkFiles\LinkNSN" & Trim(LoginForm1.Ctry.Text) & ".xml")
Thank you  for the clarification.
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.
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")