• SharpDevelop 属性服务


    本篇分析Core\Services的PropertyService类,该服务是用于所有需要属性持久的地方。
    因为涉及到属性持久化的存储位置,所以类中存在5个静态常量,很重要:
            readonly static string propertyFileName = "SharpDevelopProperties.xml";
            
    readonly static string propertyFileVersion = "1.1";
            
    readonly static string propertyXmlRootNodeName = "SharpDevelopProperties";

            
    readonly static string defaultPropertyDirectory = Application.StartupPath +
                                                              Path.DirectorySeparatorChar 
    + ".." +
                                                              Path.DirectorySeparatorChar 
    + "data" +
                                                              Path.DirectorySeparatorChar 
    + "options";

            
    readonly static string configDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                                                     Path.DirectorySeparatorChar 
    + ".ICSharpCode" +
                                                     Path.DirectorySeparatorChar 
    + "SharpDevelop" +
                                                     Path.DirectorySeparatorChar;
    持久化文件名称为SharpDevelopProperties.xml,该文件中,只读取Version=1.1以及根节点为"SharpDevelopProperties"的那一部分内容。
    至于
    defaultPropertyDirectory configDirectory ,则分别是默认属性和个性化属性,一般先在configDirectory下找,找到了直接加载或保存,找不到再去defaultPropertyDirectory目录下找。

    方法很多,分为两条线:
    1.在构造函数中加载属性 LoadProperties():
            void LoadProperties()
            {
                
    if (!Directory.Exists(configDirectory))
                {
                    Directory.CreateDirectory(configDirectory);
                }

                
    if (!LoadPropertiesFromStream(configDirectory + propertyFileName))
                {
                    Console.WriteLine(
    "Can't read global properties in user path, using default properties");
                    
    if (!LoadPropertiesFromStream(defaultPropertyDirectory + Path.DirectorySeparatorChar + propertyFileName))
                    {
                        
    throw new PropertyFileLoadException();
                    }
                }
            }
    LoadPropertiesFromStream()方法检测文件是否存在。

    2.在卸载服务UploadService时保存属性,SaveProperties():
            void SaveProperties()
            {
                WritePropertiesToFile(configDirectory 
    + propertyFileName);
            }

            
    void WritePropertiesToFile(string fileName)
            {
                XmlDocument doc 
    = new XmlDocument();
                doc.LoadXml(
    "<?xml version=\"1.0\"?>\n<" + propertyXmlRootNodeName + " fileversion = \"" + propertyFileVersion + "\" />");

                doc.DocumentElement.AppendChild(ToXmlElement(doc));

                FileUtilityService fileUtilityService 
    = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService));
                fileUtilityService.ObservedSave(
    new NamedFileOperationDelegate(doc.Save), fileName, FileErrorPolicy.ProvideAlternative);
            }
    这里使用了File服务的保证例程安全的文件保存方法——
    ObservedSave()



  • 相关阅读:
    Python之 continue继续循环
    Python之 break退出循环
    Python之 while循环
    Python之 for循环
    java中collection、map、set、list简介 (转)
    CloudSim介绍和使用
    智能指针和动态内存
    boost signal2
    boost 信号 :
    boost库 线程使用
  • 原文地址:https://www.cnblogs.com/Jax/p/834223.html
Copyright © 2020-2023  润新知