Click here to Skip to main content
15,941,071 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi All,

I have written a code to open a file as well saving it to any location of my wish.
But the problem is i want to read a file and then save it to specific location ie on server in a particular folder. My code is below. Can any one help me with the solution.
MSIL
private void bOpenFileDialog_Click(object sender, RoutedEventArgs e)
{
    // Create an instance of the open file dialog box.
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    // Set filter options and filter index.
    openFileDialog1.Filter = "All Files (*.*)|*.*";
    openFileDialog1.FilterIndex = 1;
    openFileDialog1.Multiselect = true;


    // Call the ShowDialog method to show the dialog box.
  //  bool? userClickedOK = openFileDialog1.ShowDialog();

    // Process input if the user clicked OK.
  if (openFileDialog1.ShowDialog() == true)
      using (StreamReader reader = openFileDialog1.File.OpenText())
      {
          val = reader.ReadToEnd();
          tbResults.Text = openFileDialog1.File.Name;
          reader.Close();
      }

    }

private void bSaveFileDialog_Click(object sender, RoutedEventArgs e)
{
    SaveFileDialog dlg = new SaveFileDialog();
    dlg.Filter = "XML Files (.xml)|*.xml";
    dlg.DefaultExt = "*.xml";

    // if the user doesn't cancel
    if (dlg.ShowDialog() == true)
    {
        try
        {
            // create a StreamWriter over the base stream returned by SaveFileDialog's OpenFile() method
            using (StreamWriter writer = new StreamWriter(dlg.OpenFile()))
            {
                writer.Write(val);
                writer.Close();
            }
            MessageBox.Show("File Save Successful");
        }
        catch (Exception ex) // catch all, ideally you should catch specific exceptions in most-specific to general
        {
            MessageBox.Show(ex.ToString());
        }
    }
Posted

1 solution

As long as your application user has access to the directory you wish to save to and you are running with elevated permissions, then you should just be able to go ahead an do it with out the SaveFileDialog?
If that isn't your problem, what is?
 
Share this answer
 
v2

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