欢迎来到天天文库
浏览记录
ID:34512166
大小:167.78 KB
页数:38页
时间:2019-03-07
《c#入门代码集》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C#入门代码集一、从控制台读取东西代码片断:usingSystem;classTestReadConsole{publicstaticvoidMain(){Console.Write("Enteryourname:");stringstrName=Console.ReadLine();Console.WriteLine("Hi"+strName);}}二、读文件代码片断:usingSystem;usingSystem.IO;publicclassTestReadFile{publicstaticvoidMain(String[]args){//Readtextfil
2、eC:temptest.txtFileStreamfs=newFileStream(@"c:temptest.txt",FileMode.Open,FileAccess.Read);StreamReadersr=newStreamReader(fs);Stringline=sr.ReadLine();while(line!=null){Console.WriteLine(line);line=sr.ReadLine();}sr.Close();fs.Close();}}三、写文件代码:usingSystem;usingSystem.IO;publicclas
3、sTestWriteFile{publicstaticvoidMain(String[]args){//CreateatextfileC:temptest.txtFileStreamfs=newFileStream(@"c:temptest.txt",FileMode.OpenOrCreate,FileAccess.Write);StreamWritersw=newStreamWriter(fs);//WritetothefileusingStreamWriterclasssw.BaseStream.Seek(0,SeekOrigin.End);sw.Wri
4、teLine("FirstLine");sw.WriteLine("SecondLine");sw.Flush();}}四、拷贝文件:usingSystem;usingSystem.IO;classTestCopyFile{publicstaticvoidMain(){File.Copy("c:\temp\source.txt","C:\temp\dest.txt");}}五、移动文件:usingSystem;usingSystem.IO;classTestMoveFile{publicstaticvoidMain(){File.Move("c:\temp
5、\abc.txt","C:\temp\def.txt");}}六、使用计时器:usingSystem;usingSystem.Timers;classTestTimer{publicstaticvoidMain(){Timertimer=newTimer();timer.Elapsed+=newElapsedEventHandler(DisplayTimeEvent);timer.Interval=1000;timer.Start();timer.Enabled=true;while(Console.Read()!='q'){}}publicstaticvoi
6、dDisplayTimeEvent(objectsource,ElapsedEventArgse){Console.Write("r{0}",DateTime.Now);}}七、调用外部程序:classTest{staticvoidMain(string[]args){System.Diagnostics.Process.Start("notepad.exe");}}八、连接Access数据库:usingSystem;usingSystem.Data;usingSystem.Data.OleDb;classTestADO{staticvoidMain(string
7、[]args){stringstrDSN="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb";stringstrSQL="SELECT*FROMemployees";OleDbConnectionconn=newOleDbConnection(strDSN);OleDbCommandcmd=newOleDbCommand(strSQL,conn);OleDbDataReaderreader=null;try{conn.Open();reader=cmd.ExecuteReader();while(
此文档下载收益归作者所有