CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

如何将ini中连接字串读到模块中的常量中

楼主mimihuhu(团队精神也很重要)2003-09-03 11:46:36 在 VB / 基础类 提问

我在global中定义了常量constr,原来是写死的,现在想从ini中读取,如何实现,主要是先后顺序,在模块中可以写读取操作的代码吗? 问题点数:50、回复次数:5Top

1 楼lihonggen0(李洪根,MS MVP,标准答案来了)回复于 2003-09-03 11:53:41 得分 0

建立与读取.ini文件    
     
  '请於form中放3个TextBox,一个CommandBox  
  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   success   As   Long  
  success   =   WritePrivateProfileString("MyApp",   "text1",   Text1.Text,   "c:\aa.ini")  
  '叁数一   Section   Name  
  '叁数二   於.ini中的项目  
  '叁数三   项目的内容  
  '叁数四   .ini文件的名称  
  success   =   WritePrivateProfileString("MyApp",   "text2",   Text2.Text,   "c:\aa.ini")  
  success   =   WritePrivateProfileString("MyApp2",   "text3",   Text3.Text,   "c:\aa.ini")  
  End   Sub  
   
  Private   Sub   Form_load()  
  Dim   ret   As   Long  
  Dim   buff   As   String  
  buff   =   String(255,   0)  
  ret   =   GetPrivateProfileString("Myapp",   "text1",   "text1",   buff,   256,   "c:\aa.ini")  
  '若.ini   MyApp中无text1,则采用叁数三的值  
  Text1.Text   =   buff  
  buff   =   String(255,   0)  
  ret   =   GetPrivateProfileString("Myapp",   "text2",   "text2",   buff,   256,   "c:\aa.ini")  
  Text2.Text   =   buff  
  buff   =   String(255,   0)  
  ret   =   GetPrivateProfileString("Myapp2",   "text3",   "text3",   buff,   256,   "c:\aa.ini")  
  Text3.Text   =   buff  
  End   Sub  
       
     
  Top

2 楼tonylk(=www.tonixsoft.com=)回复于 2003-09-03 12:01:23 得分 10

VERSION   1.0   CLASS  
  BEGIN  
      MultiUse   =   -1     'True  
      Persistable   =   0     'NotPersistable  
      DataBindingBehavior   =   0     'vbNone  
      DataSourceBehavior     =   0     'vbNone  
      MTSTransactionMode     =   0     'NotAnMTSObject  
  END  
  Attribute   VB_Name   =   "clsInifile"  
  Attribute   VB_GlobalNameSpace   =   False  
  Attribute   VB_Creatable   =   True  
  Attribute   VB_PredeclaredId   =   False  
  Attribute   VB_Exposed   =   False  
  Option   Explicit  
  '***************************************************************  
  'IniFile   class  
  '       2003.06.04           tony  
  '       lastmodified       2003.08.26  
  '***************************************************************  
   
  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   Declare   Function   GetPrivateProfileInt   Lib   "kernel32"   Alias   "GetPrivateProfileIntA"   (ByVal   lpApplicationName   As   String,   ByVal   lpKeyName   As   String,   ByVal   nDefault   As   Long,   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  
   
  Private   mFileName   As   String  
   
  Public   Sub   SetFileName(AFileName   As   String)  
          mFileName   =   AFileName  
  End   Sub  
   
  Public   Sub   SetString(ASection   As   String,   AItem   As   String,   AValue   As   String)  
          Call   WritePrivateProfileString(ASection,   AItem,   AValue,   mFileName)  
  End   Sub  
   
  Public   Sub   Setlong(ASection   As   String,   AItem   As   String,   AValue   As   Long)  
          Dim   ValueTmp   As   String  
           
          ValueTmp   =   Str(AValue)  
          Call   WritePrivateProfileString(ASection,   AItem,   ValueTmp,   mFileName)  
  End   Sub  
   
  Public   Sub   SetBoolean(ASection   As   String,   AItem   As   String,   AValue   As   Boolean)  
          Dim   IntVal   As   Long  
           
          If   AValue   Then  
                  IntVal   =   1  
          Else  
                  IntVal   =   0  
          End   If  
          Call   Setlong(ASection,   AItem,   IntVal)  
  End   Sub  
   
  Public   Function   GetString(ASection   As   String,   AItem   As   String,   ADefault   As   String)   As   String  
          Dim   Buff   As   String  
          Dim   BuffSize   As   Long  
           
          BuffSize   =   700  
          Buff   =   String(BuffSize,   Chr(0))  
   
          Call   GetPrivateProfileString(ASection,   AItem,   ADefault,   Buff,   BuffSize,   mFileName)  
          BuffSize   =   InStr(Buff,   Chr(0))  
           
          GetString   =   Trim(Left(Buff,   BuffSize   -   1))  
  End   Function  
   
  Public   Function   Getlong(ASection   As   String,   AItem   As   String,   ADefault   As   Long)   As   Long  
          Getlong   =   GetPrivateProfileInt(ASection,   AItem,   ADefault,   mFileName)  
  End   Function  
   
  Public   Function   GetBoolean(ASection   As   String,   AItem   As   String,   ADefault   As   Boolean)   As   Boolean  
          Dim   IntVal   As   Long  
          Dim   RetVal   As   Long  
           
          If   ADefault   Then  
                  IntVal   =   1  
          Else  
                  IntVal   =   0  
          End   If  
          RetVal   =   Getlong(ASection,   AItem,   IntVal)  
          If   RetVal   =   0   Then  
                  GetBoolean   =   False  
          Else  
                  GetBoolean   =   True  
          End   If  
  End   Function  
  Top

3 楼tonylk(=www.tonixsoft.com=)回复于 2003-09-03 12:01:59 得分 10

调用:  
  Private   Sub   LoadSettings()  
          Dim   Ini   As   New   clsInifile  
           
          On   Error   GoTo   ErrHandler  
          Call   Ini.SetFileName(AddSplash(App.Path)   &   ConfigFileName)  
          BusSpeed   =   Ini.Getlong("General",   "BusSpeed",   50)  
          BusDelay1   =   Ini.Getlong("General",   "BusDelay1",   1)  
          BusDelay2   =   Ini.Getlong("General",   "BusDelay2",   1)  
          TicketFlashTime   =   Ini.Getlong("General",   "TicketFlashTime",   1)  
          TicketDelayTime   =   Ini.Getlong("General",   "TicketDelayTime",   60)  
          GlideSpeed   =   Ini.Getlong("General",   "GlideSpeed",   50)  
           
          CommBaudRate   =   Ini.Getlong("Comm",   "CommBaudRate",   115200)  
          Exit   Sub  
           
  ErrHandler:  
          Resume   Next  
  End   Sub  
  Top

4 楼cuizm(射天狼 http://www.j2soft.cn/)回复于 2003-09-03 12:12:53 得分 10

Private   Declare   Function   ShellExecute   Lib   "shell32.dll"   Alias   "ShellExecuteA"   (ByVal   hwnd   As   Long,   ByVal   lpOperation   As   String,   ByVal   lpFile   As   String,   ByVal   lpParameters   As   String,   ByVal   lpDirectory   As   String,   ByVal   nShowCmd   As   Long)   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   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   Sub   Form_Load()  
          Open   "C:\aa.ini"   For   Output   As   #1  
          Print   #1,   "文件内容"  
          Close   #1  
  End   Sub  
   
  Private   Sub   Label1_Click()  
  On   Error   GoTo   Errhandle  
          ShellExecute   Me.hwnd,   "open",   "www.sohu.com",   vbNullString,   vbNullString,   1  
          Exit   Sub  
  Errhandle:  
          MsgBox   Err.Description   &   "!",   vbInformation  
  End   Sub  
   
  '写INI文件  
  Private   Sub   Command1_Click()  
          Dim   Counter   As   Long  
   
          For   Counter   =   1   To   4  
                  Call   WriteToIni(App.Path   &   "\Options.ini",   "Test",   "Name"   &   Counter,   "Value"   &   Counter)  
          Next   Counter  
  End   Sub  
   
  '读INI文件  
  Private   Sub   Command2_Click()  
          Dim   Counter   As   Long  
          Dim   Value(3)   As   String  
   
          For   Counter   =   1   To   4  
                  Value(Counter   -   1)   =   ReadFromIni(App.Path   &   "\Options.ini",   "Test",   "Name"   &   Counter)  
          Next   Counter  
  End   Sub  
  Top

5 楼m366(掩耳盗铃)回复于 2003-09-11 09:32:37 得分 20

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  
  Top

相关问题

  • C#中字符常量
  • 关于字符常量的问题!
  • 关于字符串常量数组
  • 有关字符串常量的疑惑。
  • 如何定义长字符串常量?
  • 未结束的字符串常量?
  • 字符型常量的指数形式?
  • 字符常量如何表示?
  • ------常量-------
  • 常量

关键词

  • ini
  • writeprivateprofilestring
  • byval
  • myapp
  • success
  • text

得分解答快速导航

  • 帖主:mimihuhu
  • tonylk
  • tonylk
  • cuizm
  • m366

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo