This article analyzes the propertyservice class of core \ Services. This service is used for all the places where property persistence is required.
Because it involves the storage location of property persistence, there are five static constants in the class, which are very important: Readonly static string propertyfilename = "sharpdevelopproperties. xml ";
Readonly static string propertyfileversions = "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;
The persistent file name is sharpdevelopproperties. xml. In this file, only the part of the content whose version is 1.1 and whose root node is "sharpdevelopproperties" is read.
For defaultpropertydirectory and configdirectory, they are the default and personalized attributes. Generally, you can find them under configdirectory and find them to load or save them directly. If you cannot find them, you can find them under the defaproperpropertydirectory directory.
There are many methods, divided into two lines:
1. Load the property loadproperties () in the constructor (): 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 ();
}
}
}
The loadpropertiesfromstream () method checks whether a file exists.
2. Save the properties when uninstalling the 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) servicemanager. Services. getservice (typeof (fileutilityservice ));
Fileutilityservice. observedsave (New namedfileoperationdelegate (Doc. Save), filename, fileerrorpolicy. providealternative );
}
The file storage method-observedsave (), which ensures routine security of the file service, is used here ().