Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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.
 
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 2-Jan-13 2:12am
Comments
Sampath Kumar Sathiya at 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 at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You could try something like this - note that it assumes you can work out what the filename should be...
 
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.");
            }
        }
    }
}
  Permalink  
v2
Comments
omisheikh at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 530
1 mhegazy94 460
2 Ravi Bhavnani 230
3 Kornfeld Eliyahu Peter 185
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 7,205
1 OriginalGriff 6,801
2 CPallini 5,350
3 George Jonsson 3,644
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100