偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

學(xué)習(xí)C#數(shù)據(jù)類型:string

開發(fā) 后端
string是各種編程語(yǔ)言中最基礎(chǔ)的數(shù)據(jù)類型,本文介紹了C#數(shù)據(jù)類型中的string,供大家參考。

string是各種編程語(yǔ)言中最基礎(chǔ)的數(shù)據(jù)類型,長(zhǎng)期以來(lái)受盡其它類的壓迫,經(jīng)常被肢解(Substring、Split)、蹂躪(Join)...

而現(xiàn)在C#數(shù)據(jù)類型string要“翻身鬧革命”了,它幾乎無(wú)所不能,可以為所欲為,令其它類心驚膽顫...

讓我們來(lái)看一下革命后的string做了些什么?

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

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

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

  1. public static void Open(this string s)  
  2. {  
  3.     Process.Start(s);  

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

2. 文件及目錄操作

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

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

  1. public static void CreateDirectory(this string path)  
  2.  {  
  3.      Directory.CreateDirectory(path);  
  4.  }  
  5.  public static void WriteText(this string path, string contents)  
  6.  {  
  7.      File.WriteAllText(path, contents);  
  8.  }          
  9.  public static void DeleteFile(this string path)  
  10.  {  
  11.      if(File.Exists(path)) File.Delete(path);  
  12.  } 

還是感覺不過(guò)癮,想要?jiǎng)h除整個(gè)硬盤的文件,用上面的一個(gè)一個(gè)來(lái)也太麻煩了。也沒問題,看下面:

3. 執(zhí)行DOS命令,先看兩個(gè)簡(jiǎn)單的

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

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

  1. public static string ExecuteDOS(this string cmd)  
  2. {  
  3.     Process process = new Process();  
  4.     process.StartInfo.FileName = "cmd.exe";  
  5.     process.StartInfo.UseShellExecute = false;  
  6.     process.StartInfo.RedirectStandardInput = true;  
  7.     process.StartInfo.RedirectStandardOutput = true;  
  8.     process.StartInfo.RedirectStandardError = true;  
  9.     process.StartInfo.CreateNoWindow = true;  
  10.    process.Start();  
  11.    process.StandardInput.WriteLine(cmd);  
  12.    process.StandardInput.WriteLine("exit");  
  13.    return process.StandardOutput.ReadToEnd();  

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

ExecuteDOS 

  1. public static string ExecuteDOS(this string cmd, out string error)  
  2.  {  
  3.      Process process = new Process();  
  4.      process.StartInfo.FileName = "cmd.exe";  
  5.      process.StartInfo.UseShellExecute = false;  
  6.      process.StartInfo.RedirectStandardInput = true;  
  7.      process.StartInfo.RedirectStandardOutput = true;  
  8.      process.StartInfo.RedirectStandardError = true;  
  9.      process.StartInfo.CreateNoWindow = true;  
  10.      process.Start();  
  11.      process.StandardInput.WriteLine(cmd);  
  12.      process.StandardInput.WriteLine("exit");  
  13.      error = process.StandardError.ReadToEnd();  
  14.      return process.StandardOutput.ReadToEnd();  
  15.  } 

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

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

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

4. 執(zhí)行SQL

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

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

  1. public static object ExecuteScalar(this string sql, DbConnection conn)  
  2. {  
  3.     object result;  
  4.     using (DbCommand cmd = conn.CreateCommand())  
  5.     {  
  6.         cmd.Connection = conn;  
  7.         cmd.CommandText = sql;  
  8.         cmd.CommandType = System.Data.CommandType.Text;  
  9.         conn.Open();  
  10.         result = cmd.ExecuteScalar();  
  11.         conn.Close();  
  12.     }  
  13.     return result;  

還有Cast擴(kuò)展:

  1. public static T Cast< T>(this object obj)  
  2. {  
  3.     return (T)obj;  

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

C#數(shù)據(jù)類型string還可以做更多更多事情,只要你支持它!但不要給它太多太大的權(quán)力,萬(wàn)一哪天比你強(qiáng)大了...

【編輯推薦】

  1. 總結(jié)C#哈希表的用法
  2. 不一樣的入門:看C# Hello World的17種寫法
  3. 什么是WMI?及其示例
  4. 從C#到C++容易出現(xiàn)的問題解答
  5. 淺議.NET、ASP.NET和C#的關(guān)系
責(zé)任編輯:book05 來(lái)源: cnblogs
相關(guān)推薦

2009-09-01 16:35:55

C#操作String數(shù)

2009-09-04 10:16:30

C#數(shù)據(jù)類型

2011-06-08 13:35:18

C#數(shù)據(jù)類型

2009-09-07 10:48:53

C#數(shù)據(jù)類型

2009-09-11 12:00:33

C#預(yù)定義數(shù)據(jù)類型

2009-08-12 16:26:27

C#數(shù)據(jù)類型轉(zhuǎn)換

2009-08-14 13:52:18

C#判斷數(shù)據(jù)類型

2009-08-13 15:19:17

C#數(shù)據(jù)類型

2009-08-27 16:39:26

C# String類型

2009-08-14 11:15:45

C#基本數(shù)據(jù)類型

2024-03-14 11:54:37

C++數(shù)據(jù)類型

2009-08-12 16:01:32

C#動(dòng)態(tài)改變數(shù)據(jù)

2009-08-18 13:00:59

C#枚舉類型

2009-08-19 15:59:24

C#參數(shù)類型

2010-01-19 13:17:05

C++數(shù)據(jù)類型

2010-01-25 10:41:59

C++數(shù)據(jù)類型

2010-01-13 17:32:02

C++數(shù)據(jù)類型

2009-08-18 10:59:46

C#枚舉類型

2009-08-12 11:24:25

C# String對(duì)象

2009-08-10 17:25:58

C#匿名類型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)