c# winform 应用编程代码总结 10

35、读写ini文件

        [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,

                                                                                              StringBuilderlpRetur nedString,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);
        }

36、文件关联

        [STAThread]
        static void Main(string[] args
        {
            if (args.Length == 0)  
            {
                string FileExt;
                string FileType;
                string MIMEType;
                string ExecName;

                FileExt=".test";
                FileType="Test File";
                MIMEType="text/plain";
                ExecName=Application.ExecutablePath +" %1";

                RegistryKey RegKey;
                RegKey=Registry.ClassesRoot;
                RegKey=RegKey.CreateSubKey(FileExt);
                //RegKey.OpenSubKey(FileExt, true);
                RegKey.SetValue("", FileType);
                RegKey.SetValue("Content Type", MIMEType);
                RegKey=RegKey.CreateSubKey("shell\\open\\command");
                RegKey.SetValue("", ExecName);
                RegKey.Close();
                return
            }
            strFile=args[0];
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            this.richTextBox1.Dock=DockStyle.Fill;
            this.richTextBox1.LoadFile(strFile,RichTextBoxStreamType.PlainText);
        }

37、获取指定文件的图标

        [System.Runtime.InteropServices.DllImport("shell32")]
        private static extern IntPtr ExtractAssociatedIcon(IntPtr hInst,string lpIconPath,ref int lpiIcon);
        static IntPtr hIcon;
        private void Form1_Load(object sender, System.EventArgs e)
        {
            int IconIndex=0;
            hIcon=ExtractAssociatedIcon(this.Handle,Application.ExecutablePath,ref IconIndex);
        }

        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Icon icon=Icon.FromHandle(hIcon);
            e.Graphics.DrawIcon(icon,10,10);
        }

本系列文章是作者学习《Visual C#.NET 应用编程150例》(源码)心得笔记,欢迎转载,请注明原文地址,如有疑问,可以通过 278250658@qq.com 联系作者本人。

原文地址:https://www.cnblogs.com/syxchina/p/2197276.html