C # The Unicode text empty row remover
When a webpage saves text, there are often many blank lines, which are inconvenient for programming and other applications. The original format is inherited using word, and there are too many commands using other editing tools, therefore, we have compiled such an empty text row eliminator. It can be reduced to the tray icon, which is convenient to use.
Note: Text Files must first be in unicode format.
*------------------------------------------------
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. runtime. interopservices;
Using system. Windows. forms;
Using system. diagnostics;
Using system. reflection;
Using system. Data;
Using system. IO;
Using system. text;
abstract description of namespace rmblankline
{< br> // form1.
public class form1: system. windows. forms. form
{< br> private system. windows. forms. button button1;
private icon mnettrayicon = new icon ("tray. ICO ");
private notifyicon trayicon;
private contextmenu policyiconmnu;
// required designer variables.
private system. componentmodel. Container components = NULL;
Public form1 ()
{< br> // required for Windows Form Designer support
initializecomponent ();
// Initialize all elements of the Program
initializenotifyicon ();
// todo: after initializecomponent calls, add Any constructor Code
}
Private void initializenotifyicon ()
{
// Set attributes of the pallet Program
Trayicon = new notifyicon ();
Trayicon. Icon = mnettrayicon;
Trayicon. Text = "text blank row Eliminator" + "\ n" + "mossan 2004-2006 ";
Trayicon. Visible = true;
Trayicon. DoubleClick + = new system. eventhandler (this. trayicon_doubleclick );
// Define a menuitem array and assign the array to the contextmenu object at the same time
Menuitem [] mnuitms = new menuitem [4];
Mnuitms [0] = new menuitem ();
Mnuitms [0]. Text = "open (& O )";
Mnuitms [0]. Click + = new system. eventhandler (this. OpenWindow );
Mnuitms [0]. defaultitem = true;
Mnuitms [1] = new menuitem ();
Mnuitms [1]. Text = "about (& )";
Mnuitms [1]. Click + = new system. eventhandler (this. showmessage );
Mnuitms [2] = new menuitem ("-");
Mnuitms [3] = new menuitem ();
Mnuitms [3]. Text = "Quit (& C )";
Mnuitms [3]. Click + = new system. eventhandler (this. exitselect );
Policyiconmnu = new contextmenu (mnuitms );
Trayicon. contextmenu = policyiconmnu;
// Add the set contextmenu object to the tray Program
}
Private void trayicon_doubleclick (Object sender, eventargs E)
{
If (this. windowstate = formwindowstate. Minimized)
{
This. Visible = true;
This. windowstate = formwindowstate. normal;
This. Activate ();
}
Else
{
This. windowstate = formwindowstate. minimized;
This. Hide ();
}
}
Public void OpenWindow (Object sender, system. eventargs E)
{
This. Visible = true;
If (this. windowstate = formwindowstate. Minimized)
This. windowstate = formwindowstate. normal;
This. Activate ();
}
Public void showmessage (Object sender, system. eventargs E)
{
MessageBox. Show ("double-click Main Window mouth minimized" + "\ n" + "http://mossan.cnblogs.com ");
}
Public void exitselect (Object sender, system. eventargs E)
{
// Hide the icon in the pallet Program
Trayicon. Visible = false;
// Shut down the system
This. Close ();
}
Private void form1_doubleclick (Object sender, system. eventargs E)
{
This. windowstate = system. Windows. Forms. formwindowstate. minimized;
This. Hide ();
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
Private void initializecomponent ()
{
This. button1 = new system. Windows. Forms. Button ();
This. suspendlayout ();
// Button1
This. button1.dialogresult = system. Windows. Forms. dialogresult. Cancel;
This. button1.location = new system. Drawing. Point (116, 38 );
This. button1.name = "button1 ";
This. button1.size = new system. Drawing. Size (64, 24 );
This. button1.tabindex = 0;
This. button1.text = "Browse (& B )";
This. button1.click + = new system. eventhandler (this. button#click );
// Form1
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (292,103 );
This. controlbox = false;
This. Controls. Add (this. button1 );
This. maximizebox = false;
This. minimizebox = false;
This. Name = "form1 ";
This. showintaskbar = false;
This. startposition = system. Windows. Forms. formstartposition. centerscreen;
This. Text = "Unicode text empty row Eliminator ";
This. windowstate = system. Windows. Forms. formwindowstate. minimized;
This. DoubleClick + = new system. eventhandler (this. form1_doubleclick );
This. resumelayout (false );
}
# Endregion
/// Main entry point of the application.
[Stathread]
Static void main ()
{
// Obtain the running routine
Process instance = runninginstance ();
If (instance = NULL)
{
// If there are no other routines, create a new form
Application. Run (New form1 ());
}
Else
{
// Process the detected routine
Handlerunninginstance (instance );
}
}
Public static process runninginstance ()
{
Process current = process. getcurrentprocess ();
Process [] processes = process. getprocessesbyname (current. processname );
// Traverse a routine with the same name running
Foreach (Process in processes)
{
// Ignore existing routines
If (process. ID! = Current. ID)
{
// Ensure that the routine runs from the EXE file
If (assembly. getexecutingassembly (). Location. Replace ("/", "\") = current. mainmodule. filename)
{
// Return another routine instance
Return process;
}
}
}
// If no other routine exists, null is returned.
Return NULL;
}
Public static void handlerunninginstance (process instance)
{
// Ensure that the window is not minimized or maximized
Showwindowasync (instance. main1_whandle, ws_shownormal );
// Set the actual routine to foreground window
Setforegroundwindow (instance. main1_whandle );
}
[Dllimport ("user32.dll")]
Private Static extern bool showwindowasync (intptr hwnd, int cmdshow );
[Dllimport ("user32.dll")]
Private Static extern bool setforegroundwindow (intptr hwnd );
Private const int ws_shownormal = 1;
Private void button#click (Object sender, system. eventargs E)
{
// Open the directory dialog box and select a text file
Openfiledialog fdlg = new openfiledialog ();
Fdlg. Title = "select a text file ";
// File filtering type
Fdlg. Filter = "text file (*. txt) | *. txt | all files (*. *) | *.*";
// Default file Filter Type
Fdlg. filterindex = 1;
If (fdlg. showdialog () = dialogresult. OK)
{
String Path = fdlg. filename;
Int n = path. lastindexof ('\\');
String dir = path. substring (0, N );
String name = path. substring (n + 1, path. Length-n-1 );
Streamreader reader = NULL;
Streamwriter writer = NULL;
Encoding Unicode = encoding. Unicode;
Try
{
Reader = new streamreader (PATH );
Writer = new streamwriter ("New _" + name, false, Unicode );
String line = reader. Readline ();
While (line! = NULL)
{
If (line. Trim (). length> 0)
{
Writer. writeline (line );
}
Line = reader. Readline ();
}
MessageBox. Show ("conversion successful! ");
}
Catch
{
MessageBox. Show ("unexpected interruption! ");
Return;
}
Finally
{
If (reader! = NULL)
{
Reader. Close ();
}
If (writer! = NULL)
{
Writer. Close ();
}
}
}
}
}
}