General little things:
1. Read the code snippet from the console:
Using System;
Class TestReadConsole
{
Public static void Main ()
{
Console. Write ("Enter your name :");
String strName = Console. ReadLine ();
Console. WriteLine ("Hi" + strName );
}
}
Ii. Read File code snippets:
Using System;
Using System. IO;
Public class TestReadFile
{
Public static void Main (String [] args)
{
// Read text file C:/temp/test.txt
FileStream fs = new FileStream (@ "c:/temp/test.txt", FileMode. Open, FileAccess. Read );
StreamReader sr = new StreamReader (fs );
String line = sr. ReadLine ();
While (line! = Null)
{
Console. WriteLine (line );
Line = sr. ReadLine ();
}
Sr. Close ();
Fs. Close ();
}
}
3. Write File Code:
Using System;
Using System. IO;
Public class TestWriteFile
{
Public static void Main (String [] args)
{
// Create a text file C:/temp/test.txt
FileStream fs = new FileStream (@ "c:/temp/test.txt", FileMode. OpenOrCreate, FileAccess. Write );
StreamWriter sw = new StreamWriter (fs );
// Write to the file using StreamWriter class
Sw. BaseStream. Seek (0, SeekOrigin. End );
Sw. WriteLine ("First Line ");
Sw. WriteLine ("Second Line ");
Sw. Flush ();
}
}
4. Copy an object:
Using System;
Using System. IO;
Class TestCopyFile
{
Public static void Main ()
{
File. Copy ("c: // temp // source.txt", "C: // temp ///dest.txt ");
}
}
5. Move files:
Using System;
Using System. IO;
Class TestMoveFile
{
Public static void Main ()
{
File. Move ("c: // temp // abc.txt", "C: // temp ///def.txt ");
}
}
6. Use a Timer:
Using System;
Using System. Timers;
Class TestTimer
{
Public static void Main ()
{
Timer timer = new Timer ();
Timer. Elapsed + = new ElapsedEventHandler (DisplayTimeEvent );
Timer. Interval = 1000;
Timer. Start ();
Timer. Enabled = true;
While (Console. Read ()! = 'Q ')
{
}
}
Public static void DisplayTimeEvent (object source, ElapsedEventArgs e)
{
Console. Write ("/r {0}", DateTime. Now );
}
}
7. Call external programs:
Class Test
{
Static void Main (string [] args)
{
System. Diagnostics. Process. Start ("notepad.exe ");
}
}
ADO. NET:
8. Access database connection:
Using System;
Using System. Data;
Using System. Data. OleDb;
Class TestADO
{
Static void Main (string [] args)
{
String strDSN = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = c: // test. mdb ";
String strSQL = "SELECT * FROM employees ";
OleDbConnection conn = new OleDbConnection (strDSN );
OleDbCommand cmd = new OleDbCommand (strSQL, conn );
OleDbDataReader reader = null;
Try
{
Conn. Open ();
Reader = cmd. ExecuteReader ();
While (reader. Read ())
{
Console. WriteLine ("First Name: {0}, Last Name: {1}", reader ["FirstName"], reader ["LastName"]);
}
}
Catch (Exception e)
{
Console. WriteLine (e. Message );
}
Finally
{
Conn. Close ();
}
}
}
9. Connect to the SQL Server database:
Using System;
Using System. Data. SqlClient;
Public class TestADO
{
Public static void Main ()
{
SqlConnection conn = new SqlConnection ("Data Source = localhost; Integrated Security = SSPI; Initial Catalog = pubs ");
SqlCommand cmd = new SqlCommand ("SELECT * FROM employees", conn );
Try
{
Conn. Open ();
Reader. Close ();
Conn. Close ();
}
Catch (Exception e)
{
Console. WriteLine ("Exception Occured --> {0}", e );
}
}
}
10. read data from SQL to XML:
Using System;
Using System. Data;
Using System. Xml;
Using System. Data. SqlClient;
Using System. IO;
Public class TestWriteXML
{
Public static void Main ()
{
String strFileName = "c:/temp/output. xml ";
SqlConnection conn = new SqlConnection ("server = localhost; uid = sa; pwd =; database = db ");
String strSql = "SELECT FirstName, LastName FROM employees ";
SqlDataAdapter adapter = new SqlDataAdapter ();
Adapter. SelectCommand = new SqlCommand (strSql, conn );
// Build the DataSet
DataSet ds = new DataSet ();
Adapter. Fill (ds, "employees ");
// Get a FileStream object
FileStream fs = new FileStream (strFileName, FileMode. OpenOrCreate, FileAccess. Write );
// Apply the WriteXml method to write an XML document
Ds. WriteXml (fs );
Fs. Close ();
}
}
11. add data to the database using ADO:
Using System;
Using System. Data;
Using System. Data. OleDb;
// Create Objects of ADOConnection and ADOCommand
OleDbConnection conn = new OleDbConnection (strDSN );
OleDbCommand cmd = new OleDbCommand (strSQL, conn );
Try
{
Conn. Open ();
Cmd. ExecuteNonQuery ();
}
Catch (Exception e)
{
Console. WriteLine ("Oooops. I did it again:/n {0}", e. Message );
}
Finally
{
Conn. Close ();
}
}
}
12. Use OLEConn to connect to the database:
Using System;
Using System. Data;
Using System. Data. OleDb;
This. AutoScaleBaseSize = new System. Drawing. Size (5, 13 );
This. ClientSize = new System. Drawing. Size (292,273 );
This. Controls. AddRange (new System. Windows. Forms. Control [] {
This. textBox1,
This. button1 });
This. Name = "Form1 ";
This. Text = "Form1 ";
This. ResumeLayout (false );
}
# Endregion
[STAThread]
Static void Main ()
{
Application. Run (new SimpleForm ());
}
}
16. display the custom icon during running:
// Load icon and set to form
System. Drawing. Icon ico = new System. Drawing. Icon (@ "c:/temp/app. ico ");
This. Icon = ico;
17. Add the component to ListBox:
Private void Form1_Load (object sender, System. EventArgs e)
{
String str = "First item ";
Int I = 23;
Float flt = 34.98f;
ListBox1.Items. Add (str );
ListBox1.Items. Add (I. ToString ());
ListBox1.Items. Add (flt. ToString ());
ListBox1.Items. Add ("Last Item in the List Box ");
}
Network:
18. Obtain the IP Address:
Using System;
Using System. Net;
Class GetIP
{
Public static void Main ()
{
IPHostEntry ipEntry = Dns. GetHostByName ("localhost ");
IPAddress [] IpAddr = ipEntry. AddressList;
For (int I = 0; I <IpAddr. Length; I ++)
{
Console. WriteLine ("IP Address {0 }:{ 1}", I, IpAddr [I]. ToString ());
}
}
}
19. Obtain the machine Name:
Using System;
Using System. Net;
Class GetIP
{
Public static void Main ()
{
Console. WriteLine ("Host name: {0}", Dns. GetHostName ());
}
}
20. send an email:
Using System;
Using System. Web;
Using System. Web. Mail;
Public class TestSendMail
{
Public static void Main ()
{
Try
{
// Construct a new mail message
MailMessage message = new MailMessage ();
Message. From = "from@domain.com ";
Message. To = "pengyun@cobainsoft.com ";
Message. Cc = "";
Message. Bcc = "";
Message. Subject = "Subject ";
Message. Body = "Content of message ";
// If you want attach file with this mail, add the line below
Message. Attachments. Add (new MailAttachment ("c: // attach.txt", MailEncoding. Base64 ));
// Send the message
SmtpMail. Send (message );
System. Console. WriteLine ("Message has been sent ");
}
# Region Windows Form Designer generated code
Private void InitializeComponent ()
{
This. AutoScaleBaseSize = new System. Drawing. Size (5, 13 );
This. ClientSize = new System. Drawing. Size (292,273 );
This. Name = "Form1 ";
This. Text = "Form1 ";
This. Paint + = new System. Windows. Forms. PaintEventHandler (this. Form1_Paint );
}
# Endregion
[STAThread]
Static void Main ()
{
Application. Run (new Form1 ());
}
Private void formatepaint (object sender, System. Windows. Forms. PaintEventArgs e)
{
Graphics g = e. Graphics;
G. DrawLine (new Pen (Color. Blue), 10, 10, 210,110 );
G. DrawRectangle (new Pen (Color. Red), 200,100 );
G. DrawEllipse (new Pen (Color. Yellow), 10,150,200,100 );
}
}
XML:
23. Read XML files:
Using System;
Using System. Xml;
Class TestReadXML
{
Public static void Main ()
{
XmlTextReader reader = new XmlTextReader ("C: // test. xml ");
Reader. Read ();
While (reader. Read ())
{
Reader. MoveToElement ();
Console. WriteLine ("XmlTextReader Properties Test ");
Console. WriteLine ("========================= ");
// Read this properties of element and display them on console
Console. WriteLine ("Name:" + reader. Name );
Console. WriteLine ("Base URI:" + reader. BaseURI );
Console. WriteLine ("Local Name:" + reader. LocalName );
Console. WriteLine ("Attribute Count:" + reader. AttributeCount. ToString ());
Console. WriteLine ("Depth:" + reader. Depth. ToString ());
Console. WriteLine ("Line Number:" + reader. LineNumber. ToString ());
Console. WriteLine ("Node Type:" + reader. NodeType. ToString ());
Console. WriteLine ("Attribute Count:" + reader. Value. ToString ());
}
}
}
24. Write an XML file:
Using System;
Using System. Xml;
Public class TestWriteXMLFile
{
Public static int Main (string [] args)
{
Try
{
// Creates an XML file is not exist
XmlTextWriter writer = new XmlTextWriter ("C: // temp // xmltest. xml", null );
// Starts a new document
Writer. WriteStartDocument ();
// Write comments
Writer. WriteComment ("Commentss: XmlWriter Test Program ");
Writer. WriteProcessingInstruction ("Instruction", "Person Record ");
// Add elements to the file
Writer. WriteStartElement ("p", "person", "urn: person ");
Writer. WriteStartElement ("LastName ","");
Writer. WriteString ("Chand ");
Writer. WriteEndElement ();
Writer. WriteStartElement ("FirstName ","");
Writer. WriteString ("Mahesh ");
Writer. WriteEndElement ();
Writer. WriteElementInt16 ("age", "", 25 );
// Ends the document
Writer. WriteEndDocument ();
}
Catch (Exception e)
{
Console. WriteLine ("Exception: {0}", e. ToString ());
}
Return 0;
}
}
Web Service:
25. A small example of a Web Service:
<% @ WebService Language = "C #" Class = "TestWS" %>
Using System. Web. Services;
Public class TestWS: System. Web. Services. WebService
{
[WebMethod ()]
Public string StringFromWebService ()
{
Return "This is a string from web service .";
}
}
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