.net操作文件

Imports VB = Microsoft.VisualBasic’

'引用 函数
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer) ''API SLEEP
    Public Declare Sub writePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String)
    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, ByVal lpdwProcessId As Long) As Long
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

    '文件读取
    Public Function readFromIni(ByVal FileName As String, ByVal Section As String, ByVal Key As String) As String
        Try
            Dim i As Integer
            Dim buff As New Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(128)
            GetPrivateProfileString(Section, Key, "", buff.Value, 128, FileName)
            i = InStr(buff.Value, Chr(0))
            readFromIni = Trim(Left(buff.Value, i - 1))
        Catch ex As Exception
            errNo = 1
            MsgBox(ex.Message)
        End Try
    End Function

    '文件写入
    Public Sub writeToIni(ByVal FileName As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)
        Try
            Dim buff As New Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(128)
            buff.Value = Value & Chr(0)
            writePrivateProfileString(Section, Key, buff.Value, FileName)
        Catch ex As Exception
            MsgBox(ex.Message)
            errNo = 1
        End Try
    End Sub

'调用方法

'读取文件中的信息

serverUrl = readFromIni(My.Application.Info.DirectoryPath & "/config.dll", "Service Information", "IPAddress")
‘向文件写入信息

WriteToIni(My.Application.Info.DirectoryPath & "/config.dll", "Service Information", "IPAddress", strIPAddress)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值