1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| Using System; Using System.Drawing; Using System.Collections; Using System.ComponentModel; Using System.Windows.Forms; Using System.Data; Using System.Runtime.InteropServices; Using System.IO; Using System.Text;
Namespace Example084_读写ini文件 { Public Class Form1 : System.Windows.Forms.Form { Private System.ComponentModel.Container components = null;
Public Form1() {
InitializeComponent();
}
Protected override void Dispose( bool disposing ) { If (disposing) { If (components! = null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows Form Designer generated code Private void InitializeComponent() {
this.AutoScaleBaseSize = New System.Drawing.Size(6, 14); this.ClientSize = New System.Drawing.Size(292, 273); this.Name = "Form1"; this.Text = "Form1"; this.Closing += New System.ComponentModel.CancelEventHandler(this.Form1_Closing); this.Load += New System.EventHandler(this.Form1_Load);
} #endregion
[STAThread] Static void Main() { Application.Run(New Form1()); }
[DllImport("kernel32")] Private Static extern int GetPrivateProfileInt(String lpApplicationName,String lpKeyName,int nDefault,String lpFileName); [DllImport("kernel32")] Private Static extern bool GetPrivateProfileString(String lpApplicationName,String lpKeyName,String lpDefault,StringBuilder lpReturnedString,int nSize,String lpFileName); [DllImport("kernel32")] Private Static extern bool WritePrivateProfileString(String lpApplicationName,String lpKeyName,String lpString,String lpFileName); [DllImport("kernel32")] Private Static extern bool GetPrivateProfileSection(String lpAppName,String lpReturnedString,int nSize,String lpFileName); [DllImport("kernel32")] Private Static extern bool WritePrivateProfileSection(String lpAppName,String lpString,String lpFileName);
Public Const int MAX_PATH = 256; Public Const String FILE_NAME=".\\test.ini";
Private void Form1_Load(Object sender, System.EventArgs e) { If (File.Exists(FILE_NAME)) { StringBuilder strCaption = New StringBuilder(256); GetPrivateProfileString("Form","Caption","Default Caption",strCaption,strCaption.Capacity,FILE_NAME); this.Text=strCaption.ToString(); int myWidth = GetPrivateProfileInt("Form", "Width", this.Width, FILE_NAME); this.Width=myWidth; int myHeight = GetPrivateProfileInt("Form", "Height", this.Height, FILE_NAME); this.Height=myHeight; int myLeft = GetPrivateProfileInt("Form", "Left", this.Left, FILE_NAME); this.Left=myLeft; int myTop = GetPrivateProfileInt("Form", "Top", this.Top, FILE_NAME); this.Top=myTop; } }
Private void Form1_Closing(Object sender, System.ComponentModel.CancelEventArgs e) { String strCaption = this.Text; WritePrivateProfileString("Form","Caption",strCaption,FILE_NAME); WritePrivateProfileString("Form","Width",this.Width.ToString(),FILE_NAME); WritePrivateProfileString("Form","Height",this.Height.ToString(),FILE_NAME); WritePrivateProfileString("Form","Left",this.Left.ToString(),FILE_NAME); WritePrivateProfileString("Form","Top",this.Top.ToString(),FILE_NAME); } } }
|