View code
1 Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5 Using System. IO;
6
7 Namespace Ctest
8 {
9 Class Propertyfileoperator
10 {
11 Private Streamreader sr = Null ;
12 /// <Summary>
13 /// Constructor
14 /// </Summary>
15 /// <Param name = "strfilepath"> File Path </Param>
16 Public Propertyfileoperator ( String Strfilepath)
17 {
18 Sr = New Streamreader (strfilepath );
19 }
20 /// <Summary>
21 /// Close file stream
22 /// </Summary>
23 Public Void Close ()
24 {
25 Sr. Close ();
26 Sr = Null ;
27 }
28 /// <Summary>
29 /// Obtains the value string based on the key.
30 /// </Summary>
31 /// <Param name = "strkey"> Key </Param>
32 /// <Returns> Value </Returns>
33 Public String Getpropertiestext ( String Strkey)
34 {
35 String Strresult = String . Empty;
36 String STR = String . Empty;
37 Sr. basestream. Seek ( 0 , Seekorigin. Begin );
38 While (STR = Sr. Readline ())! = Null )
39 {
40 If (Str. substring ( 0 , Str. indexof ( ' = ' ). Equals (strkey ))
41 {
42 Strresult = Str. substring (Str. indexof ( ' = ' ) + 1 );
43 Break ;
44 }
45 }
46 Return Strresult;
47 }
48 /// <Summary>
49 /// Obtain the value Array Based on the key.
50 /// </Summary>
51 /// <Param name = "strkey"> Key </Param>
52 /// <Returns> Value Array </Returns>
53 Public String [] Getpropertiesarray ( String Strkey)
54 {
55 String Strresult = String . Empty;
56 String STR = String . Empty;
57 Sr. basestream. Seek ( 0 , Seekorigin. Begin );
58 While (STR = Sr. Readline ())! = Null )
59 {
60 If (Str. substring ( 0 , Str. indexof ( ' = ' ). Equals (strkey ))
61 {
62 Strresult = Str. substring (Str. indexof ( ' = ' ) + 1 );
63 Break ;
64 }
65 }
66 Return Strresult. Split ( ' , ' );
67 }
68 }
69 }
C # class for reading attribute files. The format of the property file should be as follows:
Property = Value
This situation applies to getpropertiestext.
Prope = value1, value2...
This situation applies to getpropertiesarray.
A test example is provided.
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Threading;
Namespace Ctest
{
Class Program
{
Static Void Main ( String [] ARGs)
{
Propertyfileoperator pro = New Propertyfileoperator ( " Test " );
String [] S = pro. getpropertiesarray ( " Test " );
Foreach ( String S1In S)
Console. writeline (S1 );
}
}
}
The property file is test.
Content is
Test = value1, vaule2, value3
Test result: