C#操作ini配置文件和写入日志操作

调用kernel32
1        [DllImport("kernel32")]
2        private static extern long WritePrivateProfileString(string section,string key, string val, string filePath);
3        [DllImport("kernel32")]
4        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
5

读取ini

 1try
 2            {
 3                if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\tif.ini"))
 4                {
 5                    IniWriteValue("Config""Lastworkfolder""tmp", System.Windows.Forms.Application.StartupPath + "\\tif.ini");
 6                    IniWriteValue("Config""Lastimgindex""-1", System.Windows.Forms.Application.StartupPath + "\\tif.ini");
 7                    WriteLog("config file is not Exists,Create config file.");
 8                }

 9                //load ini
10                Lastworkfolder = IniReadValue("Config""Lastworkfolder", System.Windows.Forms.Application.StartupPath + "\\tif.ini");
11                Lastimgindex = Convert.ToInt32(IniReadValue("Config""Lastimgindex", System.Windows.Forms.Application.StartupPath + "\\tif.ini"));
12                Firsttime = IniReadValue("Config""Firsttime", System.Windows.Forms.Application.StartupPath + "\\tif.ini");
13                //Lastimgcount = Convert.ToInt32(IniReadValue("Config", "Lastimgcount", System.Windows.Forms.Application.StartupPath + "\\tif.INI"));
14                //load ini
15                WriteLog("Read the config file.");
16                if (Lastworkfolder == "tmp")
17                {
18                    //DateTime.Now.ToString();
19                   WriteLog("Start a new work.");
20                }

21                else
22                {
23                    fileNames = Directory.GetFiles(workpath);
24                    foreach (string file in fileNames)
25                    {
26                        strImageName = strImageName + file + ",";
27                    }

28                    fileNames = (strImageName.Remove(strImageName.Length - 11)).Split(',');
29
30                    strImageName = null;
31                    for (int i = 0; i < fileNames.Length; i++)
32                    {
33                        //textBox1.AppendText(ImageNames[i].ToString() + "\n");
34
35
36                        if (fileNames[i].Substring(fileNames[i].Length - 3== "tif")
37                        {
38                            ImageCount = ImageCount + 1;
39                            strImageName = strImageName + fileNames[i] + ",";
40
41                        }

42                        if (fileNames[i].Substring(fileNames[i].Length - 3== "xml")
43                        {
44                            xmlfile = fileNames[i];
45                        }

46                    }

47                    ImageNames = (strImageName.Remove(strImageName.Length - 11)).Split(',');
48                    SetImage(ImageNames[imgcnt]);
49                    AllControlsQ(xmlfile);
50                    editsaveToolStripMenuItem.Enabled = false;
51                    toolStripStatusLabel2.Text = workpath;
52                    WriteLog("Open the last work.");
53                }

54            }

55            catch (Exception) { }

对ini文件进行操作

 1write read ini

写日志函数

 1write log


 

原文地址:https://www.cnblogs.com/tatsuya/p/673998.html