Sharpdevelop Attribute Service

Source: Internet
Author: User
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 ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.