recently i made some changes to the button "save" event and it just doenst write into the xml file or create a new one anymore. I can't seem to figure out what is wrong over here...My savepath string here looks o开发者_如何学Gok to me though. Please help me figure out what is wrong with my code. Many thanks.
Here is my code:
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
string savepath;
SaveFileDialog DialogSave = new SaveFileDialog();
// Default file extension
DialogSave.DefaultExt = "txt";
// Available file extensions
DialogSave.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*";
// Adds a extension if the user does not
DialogSave.AddExtension = true;
// Restores the selected directory, next time
DialogSave.RestoreDirectory = true;
// Dialog title
DialogSave.Title = "Where do you want to save the file?";
// Startup directory
DialogSave.InitialDirectory = @"C:/";
if (DialogSave.ShowDialog().Equals(true))
{
savepath = DialogSave.FileName;
//DialogSave.Dispose();
DialogSave = null;
FormSaving formsaving = new FormSaving();
formsaving.Builderemail = comboBox1.SelectedIndex;
formsaving.Manageremail = comboBox2.SelectedIndex;
if (MajorversionresultLabel != null && MajorversionresultLabel.Content != null && MajorversionLabel.Content.ToString() != string.Empty)
formsaving.Majorversion = MajorversionresultLabel.Content.ToString();
if (MinorversionresultLabel != null && MinorversionresultLabel.Content != null && MinorversionLabel.Content.ToString() != string.Empty)
formsaving.Minorversion = MinorversionresultLabel.Content.ToString();
if (ProjectnumberresultLabel != null && ProjectnumberresultLabel.Content != null && ProjectnumberLabel.Content.ToString() != string.Empty)
formsaving.Projectnumber = ProjectnumberresultLabel.Content.ToString();
if (BuildnumberresultLabel != null && BuildnumberresultLabel.Content != null && BuildnumberLabel.Content.ToString() != string.Empty)
formsaving.Buildnumber = BuildnumberresultLabel.Content.ToString();
if (PreviousbuildversionresultLabel != null && PreviousbuildversionresultLabel.Content != null && PreviousbuildversionresultLabel.Content.ToString() != string.Empty)
formsaving.Previousbuildversion = PreviousbuildversionresultLabel.Content.ToString();
formsaving.Startzbuildfrom = StartzbuildfromcomboBox.SelectedIndex;
formsaving.Fullorsingle = FullorsinglecomboBox.SelectedIndex;
formsaving.Builddrive = BuilddrivecomboBox.SelectedIndex;
if (TruecmtipresultTextBlock != null && TruecmtipresultTextBlock.Text != null && TruecmtipresultTextBlock.Text != string.Empty)
formsaving.Truecmtip = TruecmtipresultTextBlock.Text;
formsaving.Truecmcomments = TruecmcommentsresultTextBlock.Text;
...
using (Stream savestream = new FileStream(savepath, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
serializer.Serialize(savestream, formsaving);
}
}
DialogSave.Dispose();
}
Which SaveFileDialog class are you using ? The one from Microsoft.Win32 or the one from System.Windows.Forms ?
The first one returns bool?, so your code should be OK (except for setting DialogSave to null, then disposing it outside of the if block).
If you're using the second one, it returns a DialogResult enum, which can never equal 'true'.
I bet your original version of the code was using SelectedItem
everywhere you're using SelectedIndex
, unless you're really doing something horrible.
Also, good heavens, do you need to be using data binding.
精彩评论