怎么编辑boot.ini?

解决方案 »

  1.   

    首先声明API函数
    Public n As Integer
    Public readspace As String * 255
    Public INIfilename As String
    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As LongPublic Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long‘===============
    INIfilename = "D:\生化软件\boot.ini"
    n = GetPrivateProfileString("默认串口", "串口号", "", readspace, Len(readspace), INIfilename)其中boot.ini的内容格式应该知道吧,举个例子,该文件的内容为:
    ‘=======
    [默认串口]
    串口号=1
    [ALT]
    靶值 =4.3
    SD =0.12
    SD2 =0.34
    [AST]
    靶值 = 1
    SD = 11
    SD2 = 11
    [GGT]
    靶值 = 1
    SD = 11
    ‘=====
    上边是读取,下边介绍写入
    INIfilename = "D:\生化软件\param.ini"
    n = WritePrivateProfileString("默认串口", "串口号", Text1.Text, INIfilename)第一次来,第一次留言,不知道满意不?多指教
      

  2.   

    给你一个我做的类模块 clsIniFileOption ExplicitPrivate Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
          (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
          ByVal lpFileName As String) As LongPrivate Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
          (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, _
          ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As LongPrivate sFileName As StringPublic Property Let IniFile(ByVal sFile As String)
        sFileName = sFile
    End PropertyPublic Property Get IniFile() As String
        IniFile = sFileName
    End PropertyPublic Function ReadKey(ByVal sSection As String, ByVal sKey As String, Optional sDefault As String = vbNullString) As String
        Dim i As Long, s As String
        
        s = Space(&H4000)
        
        i = GetPrivateProfileString(sSection, sKey, sDefault, s, &H4000, sFileName)
        If Len(Trim(s)) > 1 Then
            ReadKey = Mid(Trim(s), 1, Len(Trim(s)) - 1)
        Else
            ReadKey = sDefault
        End If
    End FunctionPublic Sub WriteKey(ByVal sSection As String, ByVal sKey As String, ByVal sData As String)
        Dim i As Long
        i = WritePrivateProfileString(sSection, sKey, sData, sFileName)
    End SubPublic Sub ReadIni()   '读取Ini
        g_Sys.m_sConnect = ReadKey("aa", "dbConnStr")
    End SubPublic Sub WriteIni()  '写入Ini
        WriteKey "aa", "IfTrans", g_Sys.m_bTrans
    End Sub
      

  3.   


    Option ExplicitPrivate Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    '自定义读取INI函數
    Private Function getIni(ByVal pfileName As String, ByVal psection As String, ByVal pkey As String) As String
        Dim x As Long, Buff As String * 128, i%
        x = GetPrivateProfileString(psection, pkey, "", Buff, 128, pfileName)
        i = InStr(Buff, Chr(0))
        getIni = Trim(Left(Buff, i - 1))
    End Function'自定义写入INI函數
    Private Function WriteIni(ByVal psection As String, ByVal pkey As String, ByVal pvalue As String, ByVal filePath As String) As Boolean
        Dim x As Long, Buff As String * 128, i As Integer
        Buff = pvalue + Chr(0)
        x = WritePrivateProfileString(psection, pkey, Buff, filePath)
        WriteIni = x
    End Function
    '删除INI文件中指定的键
    Public Sub DelKeyINI(ByVal iniFileName As String, ByVal iniSection As String, ByVal iniKey As String)
        WritePrivateProfileString iniSection, iniKey, vbNullString, iniFileName
    End Sub
      

  4.   

    恩恩。太热情了大家。我目的是运行该软件后自动修改boot.ini文件。不是还要去掉boot.ini文件的只读属性么?DOS用attrib Boot.ini -h -s去掉该属性。
      

  5.   

    Private strINI As StringPrivate Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long'修改
    Public Sub WriteToIni(ByVal Section As String, ByVal Key As String, ByVal strValue As String)
        On Error Resume Next
        Dim buff As String * 1024
        buff = strValue + Chr(0)
        WritePrivateProfileString Section, Key, buff, strINI
    End Sub'读取
    Public Function ReadFromIni(ByVal Section As String, ByVal Key As String) As String
        On Error Resume Next
        Dim i As Long
        Dim buff As String * 1024
        GetPrivateProfileString Section, Key, "", buff, Len(buff), strINI
        i = InStr(buff, Chr(0))
        ReadFromIni = Trim(Left(buff, i - 1))
    End Function'***************************************清除KeyWord"键"(Sub)***********************************************
    Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
        Dim RetVal As Integer
        RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
    End Function'如果是清除section就少写一个Key多一个""。
    '**************************************清除 Section"段"(Sub)***********************************************
    Public Function DelIniSec(ByVal SectionName As String)      '清除section
        Dim RetVal As Integer
        RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)
    End Function Private Sub Form_Load()
    strINI = App.Path & "\setup.ini"
    End Sub
     
      

  6.   

    改变文件的属性,用SetAttr 语句MSDN:
    SetAttr 语句
          为一个文件设置属性信息。语法SetAttr pathname, attributesSetAttr 语句的语法含有以下这些命名参数:部分 描述 
    pathname 必要参数。用来指定一个文件名的字符串表达式,可能包含目录或文件夹、以及驱动器。 
    Attributes 必要参数。常数或数值表达式,其总和用来表示文件的属性。 
    设置值attributes 参数设置可为:常数 值 描述 
    vbNormal 0 常规(缺省值) 
    VbReadOnly 1 只读。 
    vbHidden 2 隐藏。 
    vbSystem 4 系统文件 
    vbArchive 32 上次备份以后,文件已经改变 
    注意 这些常数是由 VBA 所指定的,在程序代码中的任何位置,可以使用这些常数来替换真正的数值。说明如果想要给一个已打开的文件设置属性,则会产生运行时错误。