11 [DllImport("kernel32")]
22 private static extern long WritePrivateProfileString(string section,string key, string val, string filePath);
33 [DllImport("kernel32")]
44 private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
读取ini
1try
2 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 ini
1010 Lastworkfolder = IniReadValue("Config", "Lastworkfolder", System.Windows.Forms.Application.StartupPath + "\\tif.ini");
1111 Lastimgindex = Convert.ToInt32(IniReadValue("Config", "Lastimgindex", System.Windows.Forms.Application.StartupPath + "\\tif.ini"));
1212 Firsttime = IniReadValue("Config", "Firsttime", System.Windows.Forms.Application.StartupPath + "\\tif.ini");
1313 //Lastimgcount = Convert.ToInt32(IniReadValue("Config", "Lastimgcount", System.Windows.Forms.Application.StartupPath + "\\tif.INI"));
1414 //load ini
1515 WriteLog("Read the config file.");
1616 if (Lastworkfolder == "tmp")
1717 {
1818 //DateTime.Now.ToString();
1919 WriteLog("Start a new work.");
2020 }
2121 else
2222 {
2323 fileNames = Directory.GetFiles(workpath);
2424 foreach (string file in fileNames)
2525 {
2626 strImageName = strImageName + file + ",";
2727 }
2828 fileNames = (strImageName.Remove(strImageName.Length - 1, 1)).Split(',');
2929
3030 strImageName = null;
3131 for (int i = 0; i < fileNames.Length; i++)
3232 {
3333 //textBox1.AppendText(ImageNames[i].ToString() + "\n");
3434
3535
3636 if (fileNames[i].Substring(fileNames[i].Length - 3) == "tif")
3737 {
3838 ImageCount = ImageCount + 1;
3939 strImageName = strImageName + fileNames[i] + ",";
4040
4141 }
4242 if (fileNames[i].Substring(fileNames[i].Length - 3) == "xml")
4343 {
4444 xmlfile = fileNames[i];
4545 }
4646 }
4747 ImageNames = (strImageName.Remove(strImageName.Length - 1, 1)).Split(',');
4848 SetImage(ImageNames[imgcnt]);
4949 AllControlsQ(xmlfile);
5050 editsaveToolStripMenuItem.Enabled = false;
5151 toolStripStatusLabel2.Text = workpath;
5252 WriteLog("Open the last work.");
5353 }
5454 }
5555 catch (Exception) { }
对ini文件进行操作
1write read ini#region write read ini
2 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
写日志函数
1write 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