-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileOP.cs
134 lines (132 loc) · 4.53 KB
/
FileOP.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace FindExcelContent
{
public class FileOP
{
/// <summary>
/// 写入文件
/// </summary>
/// <param name="strSection">要写入的段落名</param>
/// <param name="strKey">要写入的键</param>
/// <param name="strValue">要写入的值</param>
/// <param name="strFilePath">INI文件的完整路径和文件名</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string strSection, string strKey, string strValue, string strFilePath);
/// <summary>
/// 读取文件
/// </summary>
/// <param name="strSection">要读取的段落名</param>
/// <param name="strKey">要读取的键</param>
/// <param name="strDefine">读取异常的情况下的默认值</param>
/// <param name="retValue">key所对应的值,如果该key不存在则返回空值</param>
/// <param name="nSize">值允许的大小</param>
/// <param name="strFilePath">INI文件的完整路径和文件名</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string strSection, string strKey, string strDefine, StringBuilder retValue, int nSize, string strFilePath);
/// <summary>
/// 文件路径
/// </summary>
private string m_filePath = string.Empty;
public FileOP()
{
}
public FileOP(string strFilePath)
{
SetFilePath(strFilePath);
}
/// <summary>
/// 设置文件路径
/// </summary>
public void SetFilePath(string strFilePath)
{
m_filePath = strFilePath;
}
/// <summary>
/// 读取文件路径
/// </summary>
public string GetFilePtah()
{
return m_filePath;
}
/// <summary>
/// 创建文件
/// </summary>
public void CreateFile(string strFilePath)
{
SetFilePath(strFilePath);
CreateFile();
}
/// <summary>
/// 创建文件
/// </summary>
public void CreateFile()
{
if (m_filePath.Length == 0) return;
if (File.Exists(m_filePath)) return;
FileStream fileStream = File.Create(m_filePath);
if (fileStream != null)
fileStream.Close();
}
/// <summary>
/// 写入字符串
/// </summary>
public void WriteString(string strSection, string strKey, string strValue)
{
WritePrivateProfileString(strSection, strKey, strValue, m_filePath);
}
/// <summary>
/// 写整形
/// </summary>
public void WriteInt(string strSection, string strKey, int intValue)
{
WriteString(strSection, strKey, intValue.ToString());
}
/// <summary>
/// 写整形
/// </summary>
public void WriteInt(string strSection, string strKey, long intValue)
{
WriteString(strSection, strKey, intValue.ToString());
}
/// <summary>
/// 写bool
/// </summary>
public void WriteBool(string strSection, string strKey, bool value)
{
string str = value ? "true" : "false";
WriteString(strSection, strKey, str);
}
/// <summary>
/// 读取字符串
/// </summary>
public string ReadString(string strSection, string strKey, string strDefine = "")
{
StringBuilder retValue = new StringBuilder(1024);
int nSize = GetPrivateProfileString(strSection, strKey, strDefine, retValue, 1024, m_filePath);
return retValue.ToString();
}
/// <summary>
/// 读取int
/// </summary>
public int ReadInt(string strSection, string strKey, int intDefine = 0)
{
string str = ReadString(strSection, strKey, intDefine.ToString());
int nValue = 0;
int.TryParse(str, out nValue);
return nValue;
}
/// <summary>
/// 读取bool
/// </summary>
public bool ReadBool(string strSection, string strKey, bool value = false)
{
string strv = value ? "true" : "false";
string str = ReadString(strSection, strKey, strv);
return str.Equals("true");
}
}
}