C#操作ini配置文件和写入日志操作
1
1 [DllImport("kernel32")]2
2 private static extern long WritePrivateProfileString(string section,string key, string val, string filePath);3
3 [DllImport("kernel32")]4
4 private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);读取ini
1
try2

2
{3
3 if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\tif.ini"))4

4
{5
5 IniWriteValue("Config", "Lastworkfolder", "tmp", System.Windows.Forms.Application.StartupPath + "\\tif.ini");6
6 IniWriteValue("Config", "Lastimgindex", "-1", System.Windows.Forms.Application.StartupPath + "\\tif.ini");7
7 WriteLog("config file is not Exists,Create config file.");8
8 }9
9 //load ini10
10 Lastworkfolder = IniReadValue("Config", "Lastworkfolder", System.Windows.Forms.Application.StartupPath + "\\tif.ini");11
11 Lastimgindex = Convert.ToInt32(IniReadValue("Config", "Lastimgindex", System.Windows.Forms.Application.StartupPath + "\\tif.ini"));12
12 Firsttime = IniReadValue("Config", "Firsttime", System.Windows.Forms.Application.StartupPath + "\\tif.ini");13
13 //Lastimgcount = Convert.ToInt32(IniReadValue("Config", "Lastimgcount", System.Windows.Forms.Application.StartupPath + "\\tif.INI"));14
14 //load ini15
15 WriteLog("Read the config file.");16
16 if (Lastworkfolder == "tmp")17

17
{18
18 //DateTime.Now.ToString();19
19 WriteLog("Start a new work.");20
20 }21
21 else22

22
{23
23 fileNames = Directory.GetFiles(workpath);24
24 foreach (string file in fileNames)25

25
{26
26 strImageName = strImageName + file + ",";27
27 }28
28 fileNames = (strImageName.Remove(strImageName.Length - 1, 1)).Split(',');29
2930
30 strImageName = null;31
31 for (int i = 0; i < fileNames.Length; i++)32

32
{33
33 //textBox1.AppendText(ImageNames[i].ToString() + "\n");34
3435
3536
36 if (fileNames[i].Substring(fileNames[i].Length - 3) == "tif")37

37
{38
38 ImageCount = ImageCount + 1;39
39 strImageName = strImageName + fileNames[i] + ",";40
4041
41 }42
42 if (fileNames[i].Substring(fileNames[i].Length - 3) == "xml")43

43
{44
44 xmlfile = fileNames[i];45
45 }46
46 }47
47 ImageNames = (strImageName.Remove(strImageName.Length - 1, 1)).Split(',');48
48 SetImage(ImageNames[imgcnt]);49
49 AllControlsQ(xmlfile);50
50 editsaveToolStripMenuItem.Enabled = false;51
51 toolStripStatusLabel2.Text = workpath;52
52 WriteLog("Open the last work.");53
53 }54
54 }55

55 catch (Exception)
{ }对ini文件进行操作
1

write read ini#region write read ini2
public void IniWriteValue(string Section, string Key, string Value, string filepath)//对ini文件进行写操作的函数3

{4
WritePrivateProfileString(Section, Key, Value, filepath);5
}6

7
public string IniReadValue(string Section, string Key, string filepath)//对ini文件进行读操作的函数8

{9

10
StringBuilder temp = new StringBuilder(200);11
long i = GetPrivateProfileString(Section, Key, "", temp, 200, filepath);12
return System.String.Format(temp.ToString());13
}14

15
#endregion写日志函数
1
write log#region write log
2
private static void WriteLog(string LogContent)
3
{
4
//get path
5
//File.Exists(System.Windows.Forms.Application.StartupPath + "log\\tif.ini"))
6
//string strPath = System.Configuration.ConfigurationSettings.AppSettings.Get("LogPath");
7
string strPath = System.Windows.Forms.Application.StartupPath + "\\log";
8
if (strPath == "")
9
return;
10
11
if (!Directory.Exists(strPath))
12
{
13
//create path
14
Directory.CreateDirectory(strPath);
15
}
16
17
string fileName = DateTime.Now.ToString("yyyyMMdd") + ".log";
18
strPath = strPath + "\\" + fileName;
19
20
string context = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " ----> " + LogContent;
21
if (File.Exists(strPath))
22
{
23
StreamWriter log_sw = File.AppendText(strPath);
24
log_sw.WriteLine(context);
25
log_sw.Close();
26
}
27
else
28
{
29
StreamWriter log_sw = File.CreateText(strPath);
30
log_sw.WriteLine(context);
31
log_sw.Close();
32
}
33
}
34
#endregion

浙公网安备 33010602011771号