csharp C#utilities - .INI文件助手类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#utilities - .INI文件助手类相关的知识,希望对你有一定的参考价值。

using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace Utilities
{
    public class IniFile
    {
        private string Path;
        private string EXE = Assembly.GetExecutingAssembly().GetName().Name;

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        private static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        private static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);

        public IniFile(string IniPath = null)
        {
            Path = new FileInfo(IniPath ?? EXE + ".ini").FullName.ToString();
        }

        public string Read(string Key, string Section = null)
        {
            var RetVal = new StringBuilder(255);
            GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
            return RetVal.ToString();
        }

        public void Write(string Key, string Value, string Section = null)
        {
            WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
        }

        public void DeleteKey(string Key, string Section = null)
        {
            Write(Key, null, Section ?? EXE);
        }

        public void DeleteSection(string Section = null)
        {
            Write(null, null, Section ?? EXE);
        }

        public bool KeyExists(string Key, string Section = null)
        {
            return Read(Key, Section).Length > 0;
        }
    }
}

以上是关于csharp C#utilities - .INI文件助手类的主要内容,如果未能解决你的问题,请参考以下文章

csharp C#utilities - .INI文件助手类

csharp C#或ASP.Net Utility.cs

csharp util csharp

csharp 如何生成具有规则的随机密码的示例。从计算机编程基础知识到C#http://www.introprogramming.i

csharp 如何生成具有规则的随机密码的示例。从计算机编程基础知识到C#http://www.introprogramming.i

csharp utils的