2013-12-16 30 views
0

在編寫一個程序,我需要序列化一個AppSettings對象,它由多個屬性組成,其中包括一個用於存儲上次使用的文件名的屬性,我發現FileName屬性放置在我的對象中(通過賦值),但不會序列化到xml文件。沒有拋出異常並且沒有數據被寫入。OpenFileDialog文件名序列化

但反過來說,如果我programmtically修改對象

tc.TheDataFile = "c:\\Documents And Settings\\SomeUser\\Sample\\a test file.txt"; 

代替

tc.TheDataFile = theDialog.FileName; 

,將工作。有人可以提供一些關於我失蹤的信息嗎?

這是一個與問題直接相關的簡單版本的程序。

測試類,在理論上舉行的AppSettings ---

[Serializable()] 
public class TestClass 
{ 
    private string m_TheDataFile; 
    private bool m_UseLastKnownDataFile = true; 


    public bool UseLastKnownDataFile 
    { 
     get 
     { 
      return m_UseLastKnownDataFile; 
     } 
     set 
     { 
      m_UseLastKnownDataFile = value; 
     } 
    } 

    public string TheDataFile 
    { 
     get 
     { 
      return m_TheDataFile; 
     } 
     set 
     { 
      m_TheDataFile = value; 
     } 
    } 
} 

public class TestClassHelper 
{ 
    public static TestClass Load() 
    { 
     XmlSerializer serializer = new XmlSerializer(typeof(TestClass)); 
     TestClass retVal; 
     TextReader reader = null; 
     bool fileNotFound = false; ; 

     try 
     { 
      reader = new StreamReader("TestClassConfig.xml"); 
     } 
     catch (FileNotFoundException) 
     { 
      fileNotFound = true; 
     } 

     if (fileNotFound) 
     { 
      retVal = new TestClass(); 
     } 
     else 
     { 
      retVal = (TestClass)serializer.Deserialize(reader); 
      reader.Close(); 
     } 

     return retVal; 
    } 

    public static void Save(TestClass settings) 
    { 
     XmlSerializer serializer = new XmlSerializer(typeof(TestClass)); 
     TextWriter writer = new StreamWriter("TestClassConfig.xml"); 
     serializer.Serialize(writer, settings); 
     writer.Close(); 
    } 
} 

在此可以將提示輸入文件名,用戶的形式。在這個測試中,有一個按鈕的表單。

public partial class Form1 : Form 
{ 

    TestClass tc = null; 

    public Form1() 
    { 
     InitializeComponent(); 

     tc = TestClassHelper.Load(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog theDialog = new OpenFileDialog(); 
     string fileName = string.Empty; 

     theDialog.CheckFileExists = true; 
     theDialog.CheckPathExists = true; 
     theDialog.Multiselect = false; 
     theDialog.FileName = string.Empty; 


     if (theDialog.ShowDialog() == DialogResult.OK) 
     { 
      tc.TheDataFile = theDialog.FileName; 
     } 
     else 
     { 
      tc.TheDataFile = string.Empty; 
     } 
    } 

    private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
    { 
     TestClassHelper.Save(tc); 
    } 
} 

編輯補充: 我使用Microsoft Visual Studio 2005 Team Edition中W /點網2.0.50727 SP1,不帶任何選項來升級開發環境。

解決方案 我不確定爲什麼發生這種情況,但OpenFileDialog控件必須更改程序的當前操作目錄。當對象被反序列化爲xml文件時,它不再寫入最初打開的位置。而是在新的目錄中創建。

我通過使XML讀取和寫入位置更具體來糾正了這個問題。

回答

1

的問題是,您在if塊之後設置tc.TheDataFile = fileName;,但你永遠不會分配什麼fileName當你把它初始化爲string.Empty除外。一個補丁修復是:

if (theDialog.ShowDialog() == DialogResult.OK) 
    { 
     fileName = theDialog.FileName; 
    } 

    // record last used data file 
    tc.TheDataFile = fileName; 

或只是

if (theDialog.ShowDialog() == DialogResult.OK) 
    { 
     tc.TheDataFile = theDialog.FileName; 
    } 

注意,運行測試在調試器和「看」荷蘭國際集團的變量會作出這個問題很容易被發現。

+0

對不起。該行代碼應該被省略。這肯定會造成問題,但這不是問題所在。你不介意沒有這行代碼再試一次嗎? – user3108410

+0

@ user3108410我無法調試您的代碼。你是否在調試器中一行一行地運行它,看看問題可能是什麼? –

+0

是的。我一直在執行代碼一直到「Save」方法中的代碼行,它將序列化數據--- serializer.Serialize(writer,settings);並且文件名值存在於「設置」變量中。然而這個xml文件沒有被修改。 – user3108410