Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone.

I want save a file to C drive, the user will select any locations to save the file. i have tried this code below and it is giving me an error on the line in bold. can you please correct me were am wrong. i also want to check if the file exist before saving. how can i do that?

this is the error am getting verify that the correct path was given


C#
string PathSelection = "";
if (PathSelection.Length > 0)
{
    txtInputfile.Text = PathSelection;
}
// i tried this
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = saveFileDialog1.FileName;
saveFileDialog1.Title = "Save Resource Files";
saveFileDialog1.CheckFileExists = true;
saveFileDialog1.CheckPathExists = true;
saveFileDialog1.DefaultExt = "resx";
saveFileDialog1.Filter = "Save Resource Files (*.resx)|*.resx";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog() == DialogResult.OK) // error here!
{
    PathSelection = saveFileDialog1.FileName;
    //string path = saveFileDialog1.FileName;
    oDataSet.WriteXml(PathSelection);
}

oDataSet.Tables.Add("Data");
Posted
Updated 8-Feb-15 22:39pm
v3
Comments
nagendrathecoder 9-Feb-15 4:31am    
What is the error?
Bacanzela 9-Feb-15 4:35am    
verify that the correct path was given
BillWoodruff 9-Feb-15 5:15am    
Where does the error occur ? Are you absolutely sure that "oDataSet" is a valid object for writing to Xml ? Why are you adding a Table to "oDataSet" after you save it ?

1 solution

There are some strange things in your source code but it works, I've not got any errors!

The things you need to fix are:

C#
string PathSelection = "";
// You have not initialized PathSelection, why shall its length > 0?
//if (PathSelection.Length > 0)
{
    //txtInputfile.Text = PathSelection;
}
// i tried this
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
// FileName has not been initialized!
//saveFileDialog1.InitialDirectory = saveFileDialog1.FileName;
 
Share this answer
 
v5
Comments
Bacanzela 9-Feb-15 4:59am    
the user will click save button and be able to name the file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900