J.C.Morris
J.C.Morris

Reputation: 803

Unique File Renaming Issue

I am developing an app right now that reads in data from a windows from and generates an XML file based on the input.

I am tasked with creating a new file each time the form is updated (User presses "Submit"). (so far so good)

Here is the catch: The file has to be named after a prominent field input. (If the user types '993388CX' in the text box, the app would rename the pending file 993388CX.xml).

I understand how to actually rename a file in C#, but not how to rename it based on a form's input. Do any classes/methods exist that will dynamically rename the file based on the form input?

Code:

//Reads info1 from user input on the app UI and generates XML statement

        XTemp = XDoc.CreateElement("New_Info");
        XTemp.InnerText = info1.Text;
        Xsource.AppendChild(XTemp);

          XDoc.Save(@"C:\oldfile.xml");

I need the new file to be renamed after the string in info1.Text

If the user input was "John5", the file needs renamed to john5.xml

Thank you

Upvotes: 2

Views: 164

Answers (3)

JohnP
JohnP

Reputation: 402

File.Move should do what you want.

Upvotes: 0

GrayFox374
GrayFox374

Reputation: 1782

    XDoc.Save(@"C:\" + info1.Text + ".xml");

Upvotes: 1

Blachshma
Blachshma

Reputation: 17395

Either directly save it with the correct name:

XDoc.Save(String.Format("C:\\{0}.xml",info1.Text));

OR

Rename it afterwards

File.Move("c:\\oldfile.xml", String.Format("C:\\{0}.xml",info1.Text));

Upvotes: 3

Related Questions