-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDllImportHelper.cs
More file actions
149 lines (135 loc) · 5.81 KB
/
Copy pathDllImportHelper.cs
File metadata and controls
149 lines (135 loc) · 5.81 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* 作用:调用外部/系统 DLL,判断文档是否处于打开状态,读取/写入 Ini 数据。
* */
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Collections.Generic;
namespace Helper.Core.Library
{
public class DllImportHelper
{
#region 私有属性常量
[DllImport("kernel32.dll")]
private static extern IntPtr _lopen(string lpPathName, int iReadWrite);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32.dll")]
private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
private const int OF_READWRITE = 2;
private const int OF_SHARE_DENY_NONE = 0x40;
private static readonly IntPtr HFILE_ERROR = new IntPtr(-1);
#endregion
#region 对外公开方法
#region 判断文档是否打开
/// <summary>
/// 判断文档是否打开
/// </summary>
/// <param name="filePath">文档路径</param>
/// <param name="existStatus">是否检查文件是否存在,true 文件不存在时判断为未打开状态</param>
/// <returns></returns>
public static bool IsFileOpen(string filePath, bool existStatus = true)
{
if (existStatus) if (!System.IO.File.Exists(filePath)) return false;
IntPtr vHandle = _lopen(filePath, OF_READWRITE | OF_SHARE_DENY_NONE);
if (vHandle == HFILE_ERROR)
{
CloseHandle(vHandle);
return true;
}
CloseHandle(vHandle);
return false;
}
/// <summary>
/// 判断文档是否打开
/// </summary>
/// <param name="existStatus">是否检查文件是否存在,true 文件不存在时判断为未打开状态</param>
/// <param name="filePathList">文档路径列表</param>
/// <returns></returns>
public static bool IsFileOpen(bool existStatus = true, params string[] filePathList)
{
foreach (string filePath in filePathList)
{
if (IsFileOpen(filePath, existStatus)) return true;
}
return false;
}
#endregion
#region 读取/设置 INI 数据
/// <summary>
/// 设置 INI 数据
/// </summary>
/// <param name="iniPath">ini 文件路径</param>
/// <param name="section">节点名,[] 符号内表示节点</param>
/// <param name="key">节点键名称,= 符号左侧表示键</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool SetIniData(string iniPath, string section, string key, string value)
{
if (!System.IO.File.Exists(iniPath)) System.IO.File.Create(iniPath).Close();
long tag = WritePrivateProfileString(section, key, value, iniPath);
if (tag > 0) return true;
return false;
}
/// <summary>
/// 设置 INI 数据
/// </summary>
/// <param name="iniPath">ini 文件路径</param>
/// <param name="section">节点名,[] 符号内表示节点</param>
/// <param name="keyValueDict">键值数据</param>
/// <returns></returns>
public static bool SetIniDataDict(string iniPath, string section, Dictionary<string, string> keyValueDict)
{
if (!System.IO.File.Exists(iniPath)) System.IO.File.Create(iniPath).Close();
if (keyValueDict != null && keyValueDict.Count > 0)
{
bool status = false;
foreach(KeyValuePair<string, string> keyValueItem in keyValueDict)
{
status = SetIniData(iniPath, section, keyValueItem.Key, keyValueItem.Value);
if (!status) return false;
}
return true;
}
return false;
}
/// <summary>
/// 获取 INI 数据
/// </summary>
/// <param name="iniPath">ini 文件路径</param>
/// <param name="section">节点名,[] 符号内表示节点</param>
/// <param name="key">节点键名称,= 符号左侧表示键</param>
/// <param name="size">读取字节长度</param>
/// <returns></returns>
public static string GetIniData(string iniPath, string section, string key, int size = 1024)
{
if (!System.IO.File.Exists(iniPath)) return null;
StringBuilder stringBuilder = new StringBuilder(size);
GetPrivateProfileString(section, key, "", stringBuilder, size, iniPath);
return stringBuilder.ToString();
}
/// <summary>
/// 获取 INI 数据
/// </summary>
/// <param name="iniPath">ini 文件路径</param>
/// <param name="section">节点名,[] 符号内表示节点</param>
/// <param name="keyList">节点键列表</param>
/// <param name="size">读取字节长度</param>
/// <returns></returns>
public static Dictionary<string, string> GetIniDataDict(string iniPath, string section, string[] keyList, int size = 1024)
{
if (keyList == null && keyList.Length == 0) return null;
Dictionary<string, string> resultDict = new Dictionary<string, string>();
foreach(string key in keyList)
{
resultDict.Add(key, GetIniData(iniPath, section, key, size));
}
return resultDict;
}
#endregion
#endregion
}
}