zoukankan      html  css  js  c++  java
  • [DllImport("kernel32.dll")]使用

    C#中读取ini配置文件 [DllImport("kernel32.dll")]

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;

    namespace ConsoleApplication1
    {
        class Program
        {
            /// <summary>
            /// 保存配置文件
            /// </summary>
            /// <param name="strAppName">配置节点名称</param>
            /// <param name="strKeyName">配置名</param>
            /// <param name="strString">配置值</param>         /// <param name="strFileName">配置文件名</param>
            /// <returns></returns>
            [DllImport("kernel32.dll")]
            public static extern bool WritePrivateProfileString(string strAppName,
                string strKeyName,
                string strDefault,
                string strFileName);
            /// <summary>
            /// 读取配置文件值
            /// </summary>
            /// <param name="strAppName">配置节点名称</param>
            /// <param name="strKeyName">配置名</param>
            /// <param name="strDefault">返回的默认值</param>
            /// <param name="sbReturnString">返回StringBuilder Cache对象</param>
            /// <param name="nSize">缓冲区大小</param>
            /// <param name="strFileName">配置文件名</param>
            /// <returns></returns>
            [DllImport("kernel32.dll")]
            public static extern bool GetPrivateProfileString(string strAppName,
                string strKeyName,
                string strDefault,
                StringBuilder sbReturnString,
                int nSize,
                string strFileName);
            /// <summary>
            /// 读取配置文件中的指定配置节点,并返回整型值
            /// </summary>
            /// <param name="strAppName">配置节点名称</param>
            /// <param name="strKeyName">配置名</param>
            /// <param name="nDefault">返回的默认值</param>
            /// <param name="strFileName">配置文件名</param>
            /// <returns></returns>
            [DllImport("Kernel32.dll")]
            public static extern int GetPrivateProfileInt(string strAppName,
                string strKeyName,
                int nDefault,
                string strFileName);
            static void Main(string[] args)
            {
                string strAppPath = "";
                string[] stringList = {"1234","b","c","d"};
                foreach(string str in stringList)
                {
                    Console.WriteLine(str);
                }
                WritePrivateProfileString("Settings", "Url", "http://dayrl.cnblogs.com%22,%22c//config.ini");

              WritePrivateProfileString("Settings", "Time", "37","c:\config.ini");
                StringBuilder sb = new StringBuilder(256);
                GetPrivateProfileString("Settings", "Url", "http://", sb, 256,"c:\config.ini");

                Console.WriteLine(sb);
                GetPrivateProfileString("Settings", "Time", "10", sb, 256,"c:\config.ini");
                Console.WriteLine(sb);
                int nReturnFromProFile = GetPrivateProfileInt("Settings", "Time", 0, "c:\config.INI");
                Console.WriteLine(nReturnFromProFile);
                Console.ReadLine();
            }
        }
    }

  • 相关阅读:
    c 中 static 关键字的作用
    关于声明变量关键字 extern 的搜索知识点
    思考在路上-虚拟机redhat系统安装tools
    一个小程序猿思考之路-头文件中#ifndef/#define/#endif作用和用法
    const 修饰的小看点(自己积点小知识)
    用css3实现闪烁效果
    icon font
    跟踪对象属性值的修改, 设置断点(Break on property change)
    setTimeout(fn, 0)引发的JavaScipt线程的思考
    "float: left;" div 不换行显示
  • 原文地址:https://www.cnblogs.com/gyc19920704/p/5431049.html
Copyright © 2011-2022 走看看