• 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()



  • 相关阅读:
    Java泛型的协变
    【LeetCode】LeetCode——第14题:Longest Common Prefix
    【BZOJ4008】【HNOI2015】亚瑟王 概率DP
    git merge 和 git merge --no-ff
    自己定义View时,用到Paint Canvas的一些温故,讲讲平时一些效果是怎么画的(基础篇 一)
    Spring Boot 集成MyBatis
    .net framework中重新注册IIS
    FireFox插件SQLite Manager的使用
    sqlite developer注册码
    javascript 中用到的时间戳函数
  • 原文地址:https://www.cnblogs.com/Jax/p/834223.html
Copyright © 2020-2023  润新知