手工睡衣的做法视频:求读写ini文件的方法和对异常处理的方法(VB)

来源:百度文库 编辑:中科新闻网 时间:2024/04/28 11:52:53
如何读写ini 文件 简明1点
如何对读取中的错误 进行处理
如 没NTFS文件系统的读权限 导致的读取错误

给你两个函数,一个是读的,一个是写的。自己慢慢研究。如果实在不明白。就加我qq93681992
void CWrite_iniDlg::OnWrite()
{
// TODO: Add your control notification handler code here
CString m_strExePath;
CString m_strIniPath;
::GetModuleFileName(NULL,m_strExePath.GetBuffer(MAX_PATH),MAX_PATH);
m_strExePath.ReleaseBuffer();
int nPos=m_strExePath.ReverseFind('\\');
m_strExePath = m_strExePath.Left(nPos);
m_strIniPath.Format("%s\\DLG.ini",m_strExePath);

CString strData;
::WritePrivateProfileString("Param","Line","1",m_strIniPath);
::WritePrivateProfileString("Param","TrackNo","2",m_strIniPath);
::WritePrivateProfileString("Param","State","3",m_strIniPath);
::WritePrivateProfileString("Param","ScreenMod","4",m_strIniPath);
}

void CWrite_iniDlg::OnRead()
{
// TODO: Add your control notification handler code here
CString m_strExePath;
CString m_strIniPath;
::GetModuleFileName(NULL,m_strExePath.GetBuffer(MAX_PATH),MAX_PATH);
m_strExePath.ReleaseBuffer();
int nPos=m_strExePath.ReverseFind('\\');
m_strExePath = m_strExePath.Left(nPos);
m_strIniPath.Format("%s\\DLG.ini",m_strExePath);

CString LineCode;
CString TrackNo;
::GetPrivateProfileString("Param","Line","",LineCode.GetBuffer(100),100,m_strIniPath);
::GetPrivateProfileString("Param","TrackNo","",TrackNo.GetBuffer(100),100,m_strIniPath);
int m_State = ::GetPrivateProfileInt("Param","State",0,m_strIniPath);
int m_ScreenMod = ::GetPrivateProfileInt("Param","ScreenMod",0,m_strIniPath);

CString textout;
textout.Format("Line=%s,TrackNo=%s,State=%d,ScreenMod=%d",LineCode,TrackNo,m_State,m_ScreenMod);
AfxMessageBox(textout);
}

我也不会!SORRY!

超级兔子就能读写ini. 呀
你可以试试!!!!

VB读写INI文件的四个函数

'文件名SourceDB.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

'以下两个函数,读/写ini文件,固定节点setting,in_key为写入/读取的主键
'仅仅针对是非值
'Y:yes,N:no,E:error
Public Function GetIniTF(ByVal In_Key As String) As Boolean
On Error GoTo GetIniTFErr
GetIniTF = True
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString "Setting", In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "1" Then
GetIniTF = True
GetStr = ""
Else
GoTo GetIniTFErr
End If
Exit Function
GetIniTFErr:
Err.Clear
GetIniTF = False
GetStr = ""
End Function

Public Function WriteIniTF(ByVal In_Key As String, ByVal In_Data As Boolean) As Boolean
On Error GoTo WriteIniTFErr
WriteIniTF = True
If In_Data = True Then
WritePrivateProfileString "Setting", In_Key, "1", App.Path & "\SourceDB.ini"
Else
WritePrivateProfileString "Setting", In_Key, "0", App.Path & "\SourceDB.ini"
End If
Exit Function
WriteIniTFErr:
Err.Clear
WriteIniTF = False
End Function

'以下两个函数,读/写ini文件,不固定节点,in_key为写入/读取的主键
'针对字符串值
'空值表示出错
Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End Function

Public Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
GoTo WriteIniStrErr
Else
WritePrivateProfileString AppName, In_Key, In_Data, App.Path & "\SourceDB.ini"
End If
Exit Function
WriteIniStrErr:
Err.Clear
WriteIniStr = False
End Function

用记事本就能打开和编写;右击鼠标-打开方式-选记事本即可。

女子厂。