I need to save an .xml file in a specific location on a computer. But location could be changed based on selection of the user.
I can get user selection (from combobox) to the variable like this:
location = (string)comboBox1.SelectedItem;
But I can’t use following command to store my file because of “%location%” part. It says “Could not find a part of the path”
docSave.Save(@"C:\...\...\%location%\...\Information.xml");
Can anyone help me regarding that….?
Thank you.
String.Format is what you are looking for.
string.Format("C:\\...{0}\\Information.xml", location);
You should always use the Path class when you're working with paths, so if you want to get a working path from multiple parts, use Path.Combine:
string location = (string)comboBox1.SelectedItem;
string dir = "C:\dir1\dir2\%location%\dir4".Replace("%location%", location);
string filename = "Information.xml";
string fullPath = Path.Combine(dir, filename);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With