2

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

Steve Konves
  • 2,648
  • 3
  • 25
  • 44
J.C.Morris
  • 803
  • 3
  • 13
  • 26

3 Answers3

3

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));
Blachshma
  • 17,097
  • 4
  • 58
  • 72
1
    XDoc.Save(@"C:\" + info1.Text + ".xml");
GrayFox374
  • 1,742
  • 9
  • 13
0

File.Move should do what you want.

Thiem Nguyen
  • 6,345
  • 7
  • 30
  • 50
JohnP
  • 402
  • 1
  • 8
  • 25