C # Programming practices

Source: Internet
Author: User
Tags bool ini int size integer reflection tostring trim visual studio 2002
Programming a recent period of time learning to use C # programming, because accustomed to Delphi, found that C # class library is still not perfect (I use the. NET Framework 1.0, I do not know what the. NET Framework 1.1 improvements), in addition, visual Studio 2002 also has imperfections, and I don't know what improvements are in Visual Studio 2003. For example, do not provide the INI file access classes, such as the input box can not be like Delphi to specify the default Input method ( Corrections: In order to control the input method,. NET class library is supported in the System.Windows.Forms.InputLanguage class, so I have to write an INI access class and a class that switches input methods based on the input method name.

List of issues:

C # INI Access class
C # Input Method switch class
Using C # to read and write files
format string
To load a custom resource from assemble
Sort the StringCollection
Bugs for the C#builder open Tools API
Dynamically set component properties using reflection
Copy a string to the Clipboard
Get version information for program Files
Dynamic execution type method using reflection dynamic loading assembly
Other issues

C # INI Access class

Using System;
Using System.IO;
Using System.Runtime.InteropServices;
Using System.Text;
Using System.Collections;
Using System.Collections.Specialized;
Using System.Windows.Forms;

Namespace Sharpplus.ini {
<summary>
A class that mimics the Tinifile of Delphi
Revised: 1.1 Revised support for the Chinese system.
1.2 Adds the Updatefile method, realizes the support to the Win9x
1.3 Adds read-write Boolean, Integer operations
1.4 Corrected write INI although successful, but will throw an exception error
1.5 ReadString returns the trim string.
1.6 Unified and enlarged the read-write buffer size
</summary>
public class IniFile {
public string FileName; INI file name
Declaring API functions for reading and writing INI files
[DllImport ("kernel32")]
private static extern bool WritePrivateProfileString (string section,string key,string val,string filePath);
[DllImport ("kernel32")]
private static extern int getprivateprofilestring (string section,string key,string def, byte[] Retval,int size,string Epath);
Class, passing the INI file name
Public IniFile (String afilename) {
To determine whether a file exists
FileInfo fileinfo=new FileInfo (afilename);
Todo: Figuring out the use of enumerations
if ((!fileinfo.exists))//| | (Fileattributes.directory in Fileinfo.attributes))
Throw ("INI file does not exist") (new ApplicationException);
Must be a full path, not a relative path
FileName = Fileinfo.fullname;
}
Write INI file
public void WriteString (string section,string ident,string Value) {
if (! WritePrivateProfileString (section, Ident,value,filename))
{
Todo: Throw a Custom exception
Throw (New ApplicationException ("Error writing INI file"));
}
}
Reading the INI file specifies
public string ReadString (String section,string Ident, string Default) {
StringBuilder Buffer = new StringBuilder (255);
Byte[] Buffer=new byte[65535];
int buflen=getprivateprofilestring (Section,ident,default,buffer, Buffer.getupperbound (0), FileName);
You must set the encoding for 0 (the system default code page), otherwise you cannot support Chinese
String s=encoding.getencoding (0). GetString (Buffer);
S=s.substring (0,buflen);
return S.trim ();
}

Read integers
public int Readinteger (string section, string Ident, int Default) {
String intstr=readstring (section, Ident, Convert.ToString (Default));
try{
Return Convert.ToInt32 (INTSTR);
}
catch (Exception ex) {
Console.WriteLine (ex. message);
return Default;
}
}

Write integer
public void Writeinteger (string section,string Ident, int Value) {
WriteString (section, Ident, value.tostring ());
}

Read Boolean
public bool Readbool (string section, String Ident, bool Default) {
Try
{
Return Convert.toboolean (ReadString (section, Ident, Convert.ToString (Default));
}
catch (Exception ex) {
Console.WriteLine (ex. message);
return Default;
}
}

[1] [2] [3] [4] [5] Next page



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.