C#中數(shù)據(jù)類型string怎么用

這篇文章主要為大家展示了“C#中數(shù)據(jù)類型string怎么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“C#中數(shù)據(jù)類型string怎么用”這篇文章吧。

創(chuàng)新互聯(lián)建站服務(wù)項目包括雨山網(wǎng)站建設(shè)、雨山網(wǎng)站制作、雨山網(wǎng)頁制作以及雨山網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,雨山網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到雨山省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

string是各種編程語言中最基礎(chǔ)的數(shù)據(jù)類型,而現(xiàn)在C#數(shù)據(jù)類型string要“翻身鬧革命”了,它幾乎無所不能,可以為所欲為,令其它類心驚膽顫...

讓我們來看一下革命后的string做了些什么?

1. 打開文件或網(wǎng)址

"c:\\t.txt".Open();  "http://www.cnblogs.com/ldp615/".Open();

怎么做到的呢?看擴(kuò)展,很簡單,直接調(diào)用調(diào)用了Process.Start函數(shù):

public static void Open(this string s)  {      Process.Start(s);  }

單單打開個文件,竊取他人信息只是初步操作,string還可以修改、刪除、創(chuàng)建文件(或目錄)

2. 文件及目錄操作

@"C:\Directory".CreateDirectory();  @"C:\Directory\readme.txt".WriteText("this file is created by string!");  @"C:\abc.txt".DeleteFile();

實現(xiàn)同樣簡單,調(diào)用File及Directory類。以下上面三個擴(kuò)展的實現(xiàn)。(當(dāng)然還可以實現(xiàn)更多文件及目錄操作,很簡單,不再給出!)

public static void CreateDirectory(this string path)   {       Directory.CreateDirectory(path);   }   public static void WriteText(this string path, string contents)   {       File.WriteAllText(path, contents);   }           public static void DeleteFile(this string path)   {       if(File.Exists(path)) File.Delete(path);   }

還是感覺不過癮,想要刪除整個硬盤的文件,用上面的一個一個來也太麻煩了。也沒問題,看下面:

3. 執(zhí)行DOS命令,先看兩個簡單的

string output1 = "del c:\\t1.txt".ExecuteDOS();  string output2 = "dir".ExecuteDOS();

實現(xiàn)也用了Process類,如下:

public static string ExecuteDOS(this string cmd)  {      Process process = new Process();      process.StartInfo.FileName = "cmd.exe";      process.StartInfo.UseShellExecute = false;      process.StartInfo.RedirectStandardInput = true;      process.StartInfo.RedirectStandardOutput = true;      process.StartInfo.RedirectStandardError = true;      process.StartInfo.CreateNoWindow = true;     process.Start();     process.StandardInput.WriteLine(cmd);     process.StandardInput.WriteLine("exit");     return process.StandardOutput.ReadToEnd();

DOS命令也會有異常發(fā)生,下面的實現(xiàn)可通過out參數(shù)返回錯誤信息:

ExecuteDOS

public static string ExecuteDOS(this string cmd, out string error)   {       Process process = new Process();       process.StartInfo.FileName = "cmd.exe";       process.StartInfo.UseShellExecute = false;       process.StartInfo.RedirectStandardInput = true;       process.StartInfo.RedirectStandardOutput = true;       process.StartInfo.RedirectStandardError = true;       process.StartInfo.CreateNoWindow = true;       process.Start();       process.StandardInput.WriteLine(cmd);       process.StandardInput.WriteLine("exit");       error = process.StandardError.ReadToEnd();       return process.StandardOutput.ReadToEnd();   }

有了這個擴(kuò)展,格式化硬盤、關(guān)機(jī)、重啟都不在話下!

"format c:".ExecuteDOS();  "shutdown -s".ExecuteDOS();  "shutdown -r".ExecuteDOS();

以上對付一般用戶的電腦足夠了,可但對程序員的電腦,他們居然把信息放進(jìn)了數(shù)據(jù)庫!同樣有辦法!

4. 執(zhí)行SQL

DbConnection conn =   int count = "select count(*) from Girlfriends".ExecuteScalar(conn).Cast< int>();

參考實現(xiàn)如下: 

public static object ExecuteScalar(this string sql, DbConnection conn)  {      object result;      using (DbCommand cmd = conn.CreateCommand())      {          cmd.Connection = conn;          cmd.CommandText = sql;          cmd.CommandType = System.Data.CommandType.Text;          conn.Open();          result = cmd.ExecuteScalar();          conn.Close();      }      return result;  }

還有Cast擴(kuò)展:

public static T Cast< T>(this object obj)  {      return (T)obj;  }

現(xiàn)在可以執(zhí)行了。結(jié)果是***  同樣還可以實現(xiàn)更多數(shù)據(jù)庫操作。

以上是“C#中數(shù)據(jù)類型string怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

標(biāo)題名稱:C#中數(shù)據(jù)類型string怎么用
標(biāo)題鏈接:http://bm7419.com/article8/jdcdip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、云服務(wù)器、品牌網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)站收錄、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計