天天看點

C# 桌面軟體開發-深入學習[2]- AY-C#人愛學不學-aaronyang技術分享

原文: C# 桌面軟體開發-深入學習[2]- AY-C#人愛學不學-aaronyang技術分享

1 :

C#

Assembly.GetEntryAssembly().GetName().Version.ToString()           

獲得值是2.1.0.0   版本這東西,做更新,錯誤記錄 都會有用的。

C# 桌面軟體開發-深入學習[2]- AY-C#人愛學不學-aaronyang技術分享

====================www.ayjs.net       楊洋    wpfui.com        ayui      ay  aaronyang=======請不要轉載謝謝了。=========

2:建立ini檔案

[DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);           
string section = "runVersion";
            string key = "Net45";
            DateTime d = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            string val = ((long)(DateTime.Now - d).TotalSeconds).ToString();
            string text = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "AY\\Profile");
            if (!System.IO.Directory.Exists(text))
            {
                System.IO.Directory.CreateDirectory(text);
            }
            string filepath = text + "\\runVersion.ini";
            WritePrivateProfileString(section, key, val, filepath);           
C# 桌面軟體開發-深入學習[2]- AY-C#人愛學不學-aaronyang技術分享

section是 塊,然後key,value,檔案路徑

獲得ini的操作

接口

[DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);           

然後調用

string text2 = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "AY\\Profile\\runVersion.ini");
            StringBuilder sb = new StringBuilder();
            GetPrivateProfileString("runVersion", "Net45", "123", sb,10000, text2);
            MessageBox.Show(sb.ToString());           
C# 桌面軟體開發-深入學習[2]- AY-C#人愛學不學-aaronyang技術分享

修改或者新增,可以了解為 Set值 的一種行為

string text3 = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "AY\\Profile\\runVersion.ini");
            WritePrivateProfileString("runVersion", "Net45", "888888", text3);
            WritePrivateProfileString("runVersion", "ayui", "7.6.1.8", text3);

            StringBuilder sb1 = new StringBuilder();
            GetPrivateProfileString("runVersion", "Net45", "", sb1, 10000, text3);
            MessageBox.Show(sb1.ToString());

            StringBuilder sb2 = new StringBuilder();
            GetPrivateProfileString("runVersion", "ayui", "", sb2, 10000, text3);
            MessageBox.Show(sb2.ToString());           

删除節點,值是null,就删除了

            string text4 = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "AY\\Profile\\runVersion.ini");

            WritePrivateProfileString("runVersion", "Net45", null, text4);

删除塊下面的所有值,當然這個塊沒有值,是以塊也是沒了

            WritePrivateProfileString("runVersion", null, null, text4);

删除所有塊

            WritePrivateProfileString(null, null, null, text4);

以上所有内容都是ay  摸索嘗試出來的。

推薦您閱讀更多有關于“”的文章