1. serialization
In. net, it is very easy to keep objects and there are many methods. It is estimated that all users use xmlserializer. In fact, there are other methods. I will introduce binaryformatter today. In mail monitorCodeIn just 10 lines of code, you can save and get it. The core is binaryformatter. Its function is to format the object in binary format. After serialize is processed, the object can be saved. To deserialize the object, the original binary data is obtained first, then use deserialize to restore. Key Issue: The [serializable] attribute must be added for each class. If the type is hashtable or custom, [xmlelement ("node name", typeof (type)] should be added. attribute.
// Keep information
Private void savesettings ()
{
Try
{
Iformatter formatter = new binaryformatter ();
Stream stream = new filestream (_ path, filemode. Create, fileaccess. Write, fileshare. None );
Formatter. serialize (stream, _ settings );
Stream. Close ();
}
Catch {}
}
// Read information
private void loadsettings ()
{< br> try
{< br> If (file. exists (_ path)
{< br> iformatter formatter = new binaryformatter ();
stream = new filestream (_ path, filemode. open, fileaccess. read, fileshare. read);
_ settings = (settings) formatter. deserialize (Stream);
stream. close ();
}< br> else
{< br> MessageBox. show (this, "configuration file not found. \ r \ nit seems that this is the first time you use mail monitor, \ r \ nplease configure it before usage. ");
}< BR >}< br> catch (exception ex)
{< br> MessageBox. show (this, Ex. message);
}< BR >}
2. Multithreading
It is easy to implement multithreading. We all know that it is implemented using the following methods:
Thread _ thread = new thread (New threadstart (getmailinfo ));
_ Thread. Start ();
The problem is that when the thread (_ thread. abort ,. net will give a thread abort exception, and the dialog box provided by this exception cannot be captured. My solution is to first _ thread. suspend, and _ thread when the window is closed. abort is enough.
3. Multi-Window operations
In many cases, we need to perform multi-window operations. For example, the main window-> Option configuration-> specific settings may need to open multiple windows, and data must be transmitted between windows, my approach is to first create a window, then pass the attributes, and then use the window. showdialog.
4. Registry
If you want to write the registry, the writable parameter must be set to true when opensubkey is enabled. Otherwise, access without permission is thrown.
5. Execution location
In addition to application. startuppath, you can directly use assembly. getentryassembly (). location to obtain the execution location.
6. Determine whether the content is a webpage and format it in a pure TXT format.
My approach is that if text contains any web page tag, it is regarded as a web page, which is flawed, but can meet most of the requirements.
Private struct htmltag
{
Internal string strprefix;
Internal string strsuffix;
}
Private Static htmltag [] _ htmltags = new htmltag [4];
Private Static string [] _ htmltagtext = {"
Public static string toformattedhtml (string strhtml)
{
For (INT I = 0; I <_ htmltagtext. length; I + = 2)
{
_ Htmltags [(INT) (I + 1)/2)]. strprefix = _ htmltagtext [I];
_ Htmltags [(INT) (I + 1)/2)]. strsuffix = _ htmltagtext [I + 1];
}
String strret = strhtml;
If (! Ishtml (strret ))
Strret = strret. Replace ("\ r \ n", "<br> \ r \ n ");
Return strret;
}
Public static bool ishtml (string strhtml)
{
String strret = strhtml. tolower ();
Bool blnret = false;
For (INT I = 0; I <_ htmltags. length; I ++)
{
If (strret. indexof (_ htmltags [I]. strprefix )! =-1 & strret. indexof (_ htmltags [I]. strsuffix )! =-1)
{
Blnret = true;
Break;
}
}
Return blnret;
}
7. obtain a valid directory/file name
Windows does not allow the directory/file name to contain invalid characters. You only need to remove these characters.
Public static string tonormalfilename (string strfile)
{
Try
{
Char [] chritems = new char [9];
Chritems [0] = '/';
Chritems [1] = '\\';
Chritems [2] = ':';
Chritems [3] = '*';
Chritems [4] = '? ';
Chritems [5] = '\"';
Chritems [6] = '<';
Chritems [7] = '> ';
Chritems [8] = '| ';
Strfile = replacechars (strfile, chritems );
}
Catch
{
}
Return strfile;
}
8. Sound
Everyone knows this method, that is, introducing the beep Win32 API
[Dllimport ("Kernel32")]
Private Static extern int BEEP (INT dwfreq, int dwduration );
Public static void beepit ()
{< br> BEEP (, 50);
}