vb.net如何在D盘新建一个test.ini文件呢?

vb.net如何在D盘新建一个test.ini文件呢?,第1张

'创建一个文本文件,把文本框里的文字写入到该文件,当然,你可以使用一个字符串变量

Dim MyStream As New System.IO.FileStream("D:\test.ini", System.IO.FileMode.Create)

Dim MyWriter As New System.IO.StreamWriter(MyStream, System.Text.Encoding.Default)

MyWriter.WriteLine(TextBox1.Text)

MyWriter.Flush()

MyWriter.Close()

MyStream.Close()

VB *** 作INI文件示例:

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

Private 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 Sub Command1_Click()

Dim A As Long

Dim B As String

B = App.Path

'写信息

'修改SET.INI文件中MAIN字段中HP的值为当前程序路径

'如果该文件不存在会自动建立,当函数返回值为0时说明修改不成功

A = WritePrivateProfileString("MAIN", "HP", B, App.Path &"\SET.INI")

If A = 0 Then MsgBox ("写文件时出错")

End Sub

Private Sub Command2_Click()

Dim A As Long

Dim T As String

'读取信息

T = Space$(1000) '事先定义读取值的字串宽度

'读取SET.INI文件中TIP字段中MAIN的值并打印出来

'当函数返回值为0时说明读取数据出错

A = GetPrivateProfileString("MAIN", "HP", "", T, 1000, App.Path &"\SET.INI")

If A = 0 Then MsgBox "找不到所需字段": Exit Sub

Print Left$(T, Len(Trim$(T)) - 1)

End Sub

'读ini

Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String

Dim ResultString As String * 144, temp As Integer

Dim S As String, i As Integer

temp% = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 144, INIFile)

If temp% >0 Then

S = ""

For i = 1 To 144

If Asc(Mid$(ResultString, i, 1)) = 0 Then

Exit For

Else

S = S + Mid$(ResultString, i, 1)

End If

Next

Else

'不存在则存为默认值

temp% = WritePrivateProfileString(SectionName, KeyWord, DefString, INIFile)

S = DefString

End If

GetIniS = S

End Function

'写ini

Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String)

Dim res%

res% = WritePrivateProfileString(SectionName, KeyWord, ValStr, INIFile)

End Sub

调用前:

INIFile=app.Path "\test1.ini"

调用时:

str1=GetIniS("A","B",text1.text) '获取值,没有则创建,并把text1.text写入


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11609024.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存