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

调用kernel32
ContractedBlock.gif ExpandedBlockStart.gif Code
1None.gif1        [DllImport("kernel32")]
2None.gif2        private static extern long WritePrivateProfileString(string section,string key, string val, string filePath);
3None.gif3        [DllImport("kernel32")]
4None.gif4        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
读取ini
ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.giftry
 2ExpandedBlockStart.gifContractedBlock.gif 2            dot.gif{
 3InBlock.gif 3                if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\tif.ini"))
 4ExpandedSubBlockStart.gifContractedSubBlock.gif 4                dot.gif{
 5InBlock.gif 5                    IniWriteValue("Config""Lastworkfolder""tmp", System.Windows.Forms.Application.StartupPath + "\\tif.ini");
 6InBlock.gif 6                    IniWriteValue("Config""Lastimgindex""-1", System.Windows.Forms.Application.StartupPath + "\\tif.ini");
 7InBlock.gif 7                    WriteLog("config file is not Exists,Create config file.");
 8ExpandedSubBlockEnd.gif 8                }

 9InBlock.gif 9                //load ini
10InBlock.gif10                Lastworkfolder = IniReadValue("Config""Lastworkfolder", System.Windows.Forms.Application.StartupPath + "\\tif.ini");
11InBlock.gif11                Lastimgindex = Convert.ToInt32(IniReadValue("Config""Lastimgindex", System.Windows.Forms.Application.StartupPath + "\\tif.ini"));
12InBlock.gif12                Firsttime = IniReadValue("Config""Firsttime", System.Windows.Forms.Application.StartupPath + "\\tif.ini");
13InBlock.gif13                //Lastimgcount = Convert.ToInt32(IniReadValue("Config", "Lastimgcount", System.Windows.Forms.Application.StartupPath + "\\tif.INI"));
14InBlock.gif14                //load ini
15InBlock.gif15                WriteLog("Read the config file.");
16InBlock.gif16                if (Lastworkfolder == "tmp")
17ExpandedSubBlockStart.gifContractedSubBlock.gif17                dot.gif{
18InBlock.gif18                    //DateTime.Now.ToString();
19InBlock.gif19                   WriteLog("Start a new work.");
20ExpandedSubBlockEnd.gif20                }

21InBlock.gif21                else
22ExpandedSubBlockStart.gifContractedSubBlock.gif22                dot.gif{
23InBlock.gif23                    fileNames = Directory.GetFiles(workpath);
24InBlock.gif24                    foreach (string file in fileNames)
25ExpandedSubBlockStart.gifContractedSubBlock.gif25                    dot.gif{
26InBlock.gif26                        strImageName = strImageName + file + ",";
27ExpandedSubBlockEnd.gif27                    }

28InBlock.gif28                    fileNames = (strImageName.Remove(strImageName.Length - 11)).Split(',');
29InBlock.gif29
30InBlock.gif30                    strImageName = null;
31InBlock.gif31                    for (int i = 0; i < fileNames.Length; i++)
32ExpandedSubBlockStart.gifContractedSubBlock.gif32                    dot.gif{
33InBlock.gif33                        //textBox1.AppendText(ImageNames[i].ToString() + "\n");
34InBlock.gif34
35InBlock.gif35
36InBlock.gif36                        if (fileNames[i].Substring(fileNames[i].Length - 3== "tif")
37ExpandedSubBlockStart.gifContractedSubBlock.gif37                        dot.gif{
38InBlock.gif38                            ImageCount = ImageCount + 1;
39InBlock.gif39                            strImageName = strImageName + fileNames[i] + ",";
40InBlock.gif40
41ExpandedSubBlockEnd.gif41                        }

42InBlock.gif42                        if (fileNames[i].Substring(fileNames[i].Length - 3== "xml")
43ExpandedSubBlockStart.gifContractedSubBlock.gif43                        dot.gif{
44InBlock.gif44                            xmlfile = fileNames[i];
45ExpandedSubBlockEnd.gif45                        }

46ExpandedSubBlockEnd.gif46                    }

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

54ExpandedBlockEnd.gif54            }

55ExpandedBlockStart.gifContractedBlock.gif55            catch (Exception) dot.gif{ }
对ini文件进行操作
1ContractedBlock.gifExpandedBlockStart.gifwrite read ini#region write read ini
 2InBlock.gif        public void IniWriteValue(string Section, string Key, string Value, string filepath)//对ini文件进行写操作的函数
 3ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 4InBlock.gif            WritePrivateProfileString(Section, Key, Value, filepath);
 5ExpandedSubBlockEnd.gif        }

 6InBlock.gif
 7InBlock.gif        public string IniReadValue(string Section, string Key, string filepath)//对ini文件进行读操作的函数
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9InBlock.gif
10InBlock.gif            StringBuilder temp = new StringBuilder(200);
11InBlock.gif            long i = GetPrivateProfileString(Section, Key, "", temp, 200, filepath);
12InBlock.gif            return System.String.Format(temp.ToString());
13ExpandedSubBlockEnd.gif        }

14InBlock.gif
15ExpandedBlockEnd.gif        #endregion

写日志函数
1ContractedBlock.gifExpandedBlockStart.gifwrite log#region   write log
 2InBlock.gif        private static void WriteLog(string LogContent)
 3ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 4InBlock.gif            //get   path
 5InBlock.gif            //File.Exists(System.Windows.Forms.Application.StartupPath + "log\\tif.ini"))
 6InBlock.gif            //string strPath = System.Configuration.ConfigurationSettings.AppSettings.Get("LogPath");
 7InBlock.gif            string strPath = System.Windows.Forms.Application.StartupPath + "\\log";
 8InBlock.gif            if (strPath == "")
 9InBlock.gif                return;
10InBlock.gif
11InBlock.gif            if (!Directory.Exists(strPath))
12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
13InBlock.gif                //create   path   
14InBlock.gif                Directory.CreateDirectory(strPath);
15ExpandedSubBlockEnd.gif            }

16InBlock.gif
17InBlock.gif            string fileName = DateTime.Now.ToString("yyyyMMdd"+ ".log";
18InBlock.gif            strPath = strPath + "\\" + fileName;
19InBlock.gif
20InBlock.gif            string context = DateTime.Now.ToString("yyyy/MM/dd   HH:mm:ss"+ "   ---->   " + LogContent;
21InBlock.gif            if (File.Exists(strPath))
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                StreamWriter log_sw = File.AppendText(strPath);
24InBlock.gif                log_sw.WriteLine(context);
25InBlock.gif                log_sw.Close();
26ExpandedSubBlockEnd.gif            }

27InBlock.gif            else
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
29InBlock.gif                StreamWriter log_sw = File.CreateText(strPath);
30InBlock.gif                log_sw.WriteLine(context);
31InBlock.gif                log_sw.Close();
32ExpandedSubBlockEnd.gif            }

33ExpandedSubBlockEnd.gif        }

34ExpandedBlockEnd.gif        #endregion

转载于:https://www.cnblogs.com/zhangchenliang/archive/2008/07/31/1257156.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值