Click here to Skip to main content
15,896,269 members

save and save as in c#

omisheikh asked:

Open original thread
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());
          }
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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