2009-08-10 9 views

答えて

1

あり、WindowsフォームでのTagプロパティはあるが、それは保持されません。私は「ない限り

public class PreferencesManager 
{ 
    static private string dataPath = null; 
    static public string DataPath 
    { 
     get 
     { 
      if (dataPath == null) 
      { 
       string baseFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); 
       dataPath = Path.Combine(baseFolder, @"MY_APP_NAME"); 

       if (!Directory.Exists(dataPath)) 
       { 
        Directory.CreateDirectory(dataPath); 
       } 
      } 

      return dataPath; 
     } 
    } 

    /// <summary> 
    /// Saves the specified preferences. 
    /// </summary> 
    /// <param name="pref">The preferences.</param> 
    static public void Save(Preferences pref) 
    { 
     // Create file to save the data to 

     string fn = "Preferences.xml"; 
     string path = Path.Combine(DataPath, fn); 
     using (FileStream fs = new FileStream(path, FileMode.Create)) 
     { 

      // Create an XmlSerializer object to perform the serialization 
      XmlSerializer xs = new XmlSerializer(typeof(Preferences)); 

      // Use the XmlSerializer object to serialize the data to the file 
      xs.Serialize(fs, pref); 
     } 
    } 

    static public Preferences Load() 
    { 
     Preferences ret = null; 
     string path = string.Empty; 

     try 
     { 
      // Open file to read the data from 

      string fn = "Preferences.xml"; 
      path = Path.Combine(DataPath, fn); 
      using (FileStream fs = new FileStream(path, FileMode.Open)) 
      { 

       // Create an XmlSerializer object to perform the deserialization 
       XmlSerializer xs = new XmlSerializer(typeof(Preferences)); 

       // Use the XmlSerializer object to deserialize the data from the file 
       ret = (Preferences)xs.Deserialize(fs); 
      } 
     } 
     catch (System.IO.DirectoryNotFoundException) 
     { 
      throw new Exception("Could not find the data directory '" + DataPath + "'"); 
     } 
     catch (InvalidOperationException) 
     { 
      return new Preferences(); 
     } 
     catch (System.IO.FileNotFoundException) 
     { 
      return new Preferences(); 
     } 

     return ret; 
    } 

} 
+0

コードスニペットで大変感謝しています:) – programmernovice

3

.NETのほとんどのUIコントロールには、同じ目的で使用できる.Tagプロパティがあります。

あなたは追加機能が必要な場合は、あなたがベースのコントロール(すなわち、あなたがのPictureBoxから継承し、追加のフィールドを追加しますSpecialPictureBoxと呼ばれるクラスを作ることができる)、および使用からを継承するクラスを作ることができますPictureBoxを使用するのと同じように、Windowsフォームにも表示されます。

+0

私が使用したパターンがプリファレンスと呼ばれるクラスを作成することであるが、このようなPreferencesManagerクラスで管理され、(私が存続したい情報ごとの特性を持っています)誤っています。.Tagは永続化されません。 –

+0

.Tagは、VB6でも永続化されません。 ;) – Brandon

関連する問題