Click here to Skip to main content
15,911,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hellow every one i have this following code this function save a list object into xml file it opens every time savefiledialoge i just want to add some code that if a file is already saved it will not open savefiledialge but over write it just like save function in ms word or some other editors. this function behaves like save as function i want both save and save as functionality if file not saved dialogue will open and if it is already saved it just over write.

C#
string file = "";

          try
          {
              if (_AllTests.Count > 0)
              {
                  // Just clear already present message
                  LogMessage(String.Empty);

                  saveFileDialog1.Filter = "XML files (*.xml)|*.xml";
                  saveFileDialog1.InitialDirectory = Application.StartupPath + "\\ SavedTest\\";

                  if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                  {
                      file = saveFileDialog1.FileName;
                      
                      foreach (Test t in _AllTests)
                      {
                          if (t._testName == tabControl2.SelectedTab.Text)
                          {
                              LogMessage("Saving " + file + "...");
                              TestToXML(t, file);
                              LogMessage("Saved Successfully.");
                          }
                      }
                  }
              }
              else
              {
                  StatusMessage("No Test Found in Test Explorer!");
                  //MessageBox.Show("No test found in Test Explorer", "Error");
              }
          }
          catch(System.Exception ex)
          {
              LogMessage("Error Occured While Saving Test in XML File!");
              DebugMessage(ex.ToString());
          }
Posted
Comments
Sampath Kumar Sathiya 2-Jan-13 8:19am    
Could you please provide the following details,

1. every time you will save the same content
2. Is it a log file
3. File Name is fixed
omisheikh 2-Jan-13 8:26am    
1-no i am not saving the same content there is different information is added every time in that list named as _AllTests i m taking my desired one test from that list
2- no its not a log file its and xml file for one test information is there actually it is a load test software and in which i can make different types of test samples and can save them in xml file so later on i can load them as it is and can use it
3- in this code file name is not fixed it is taken out from savefiledialge whatever user input at savefiledialoge it will be the name of that file
CHill60 2-Jan-13 8:42am    
It sounds as if you need the savefiledialog to find out the name of the file?? If the filename for each test cannot be derived from the name of the test in your list then you will always need the dialog. However, I will post a solution that assumes you can work out what the file is called

1 solution

You could try something like this - note that it assumes you can work out what the filename should be...

C#
if (_AllTests.Count > 0)
{
    // Just clear already present message
    LogMessage(String.Empty);
    saveFileDialog1.Filter = "XML files (*.xml)|*.xml";
    saveFileDialog1.InitialDirectory = Application.StartupPath + "\\ SavedTest\\";

    foreach (Test t in _AllTests)
    {
        if (t._testName == tabControl2.SelectedTab.Text)
        {
            DialogResult d = DialogResult.Cancel;
            FileInfo fi = new FileInfo(Application.StartupPath + "\\SavedTest\\" + t._testName);
            // amend the above line to whatever the file name should be
            if (fi.Exists())
                d = DialogResult.OK;
            else
                d = saveFileDialog1.ShowDialog         
            if(d == DialogResult.OK)   
            {
                LogMessage("Saving " + file + "...");
                TestToXML(t, file);
                LogMessage("Saved Successfully.");
            }
        }
    }
}
 
Share this answer
 
v2
Comments
omisheikh 2-Jan-13 11:54am    
thank you so much CHill60 it worked for me :)

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