01.
namespace
TORServices
02.
{
03.
public
static
class
clsINI
04.
{
05.
[DllImport(
"kernel32.dll"
, EntryPoint =
"GetPrivateProfileStringA"
, CharSet = CharSet.Ansi, SetLastError =
true
, ExactSpelling =
true
)]
06.
private
static
extern
int
GetPrivateProfileString(
string
lpApplicationName,
string
lpKeyName,
string
lpDefault, global::System.Text.StringBuilder lpReturnedString,
int
nSize,
string
lpFileName);
07.
[DllImport(
"kernel32.dll"
, EntryPoint =
"WritePrivateProfileStringA"
, CharSet = CharSet.Ansi, SetLastError =
true
, ExactSpelling =
true
)]
08.
private
static
extern
int
WritePrivateProfileString(
string
lpApplicationName,
string
lpKeyName,
string
lpString,
string
lpFileName);
09.
public
static
string
ReadValue(
string
Path,
string
section,
string
key)
10.
{
11.
global::System.Text.StringBuilder sb =
new
global::System.Text.StringBuilder(255);
12.
dynamic i = GetPrivateProfileString(section, key,
""
, sb, 255, Path);
13.
return
sb.ToString();
14.
}
15.
public
static
void
WriteValue(
string
Path,
string
section,
string
key,
string
value) { WritePrivateProfileString(section, key, value, Path); }
16.
public
static
string
textFileReader(
string
pathFileName)
17.
{
18.
string
line;
19.
StreamReader fs;
20.
try
21.
{
22.
fs =
new
StreamReader(pathFileName);
23.
line = fs.ReadToEnd();
24.
25.
26.
Encoding encodeSource = Encoding.GetEncoding(fs.CurrentEncoding.CodePage);
27.
fs.Close();
28.
29.
30.
Encoding systemEncode = Encoding.Default;
31.
Encoding targetEncode = encodeSource;
32.
33.
34.
byte
[] srcData = systemEncode.GetBytes( line );
35.
byte
[] dstData;
36.
37.
38.
if
( targetEncode != systemEncode )
39.
dstData = Encoding.Convert( systemEncode, targetEncode, srcData );
40.
else
41.
dstData = srcData;
42.
43.
44.
return
targetEncode.GetString(dstData);
45.
46.
}
47.
catch
(Exception ex)
48.
{
49.
throw
new
IOException(
"cannot find "
+ pathFileName,ex);
50.
}
51.
52.
}
53.
public
static
System.Collections.Generic.List<
string
> textFileReaderFormline(
string
pathFileName)
54.
{
55.
56.
System.Collections.Generic.List<
string
> list =
new
System.Collections.Generic.List<
string
>();
57.
System.IO.StreamReader fs;
58.
fs =
new
System.IO.StreamReader(pathFileName, System.Text.Encoding.GetEncoding(874));
59.
60.
string
line;
61.
while
((line = fs.ReadLine()) !=
null
)
62.
{
63.
list.Add(line);
64.
}
65.
return
list;
66.
67.
}
68.
}
69.
}