c#擴展方法奇思妙用變態篇四:string 的翻身革命
string是各種編程語言中最基礎的數據類型,長期以來受盡其它類的壓迫,經常被肢解(Substring、Split)、蹂躪(Join)...
而現在string要“翻身鬧革命”了,它幾乎無所不能,可以為所欲為,令其它類心驚膽顫...
讓我們來看一下革命后的string做了些什么?
1. 打開文件或網址
1 "c:\\t.txt".Open();
2 "http://www.cnblogs.com/ldp615/".Open();
2 "http://www.cnblogs.com/ldp615/".Open();
怎么做到的呢?看擴展,很簡單,直接調用調用了Process.Start函數:
1 public static void Open(this string s)
2 {
3 Process.Start(s);
4 }
2 {
3 Process.Start(s);
4 }
單單打開個文件,竊取他人信息只是初步操作,string還可以修改、刪除、創建文件(或目錄)
2. 文件及目錄操作
1 @"C:\Directory".CreateDirectory();
2 @"C:\Directory\readme.txt".WriteText("this file is created by string!");
3 @"C:\abc.txt".DeleteFile();
2 @"C:\Directory\readme.txt".WriteText("this file is created by string!");
3 @"C:\abc.txt".DeleteFile();
實現同樣簡單,調用File及Directory類。以下上面三個擴展的實現。(當然還可以實現更多文件及目錄操作,很簡單,不再給出!)
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 }
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 }
還是感覺不過癮,想要刪除整個硬盤的文件,用上面的一個一個來也太麻煩了。也沒問題,看下面:
3. 執行DOS命令,先看兩個簡單的
1 string output1 = "del c:\\t1.txt".ExecuteDOS();
2 string output2 = "dir".ExecuteDOS();
2 string output2 = "dir".ExecuteDOS();
實現也用了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();
14 }
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();
14 }
DOS命令也會有異常發生,下面的實現可通過out參數返回錯誤信息:
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif&width=11&height=16)
有了這個擴展,格式化硬盤、關機、重啟都不在話下!
1 "format c:".ExecuteDOS();
2 "shutdown -s".ExecuteDOS();
3 "shutdown -r".ExecuteDOS();
2 "shutdown -s".ExecuteDOS();
3 "shutdown -r".ExecuteDOS();
以上對付一般用戶的電腦足夠了,可但對程序員的電腦,他們居然把信息放進了數據庫!同樣有辦法!
4. 執行SQL
1 DbConnection conn = ![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/dot.gif)
2 int count = "select count(*) from Girlfriends".ExecuteScalar(conn).Cast<int>();
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/dot.gif)
2 int count = "select count(*) from Girlfriends".ExecuteScalar(conn).Cast<int>();
參考實現如下:
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;
14 }
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;
14 }
還有Cast擴展:
1 public static T Cast<T>(this object obj)
2 {
3 return (T)obj;
4 }
2 {
3 return (T)obj;
4 }
現在可以執行了。結果是*** 同樣還可以實現更多數據庫操作。
string還可以做更多更多事情,只要你支持它!但不要給它太多太大的權力,萬一哪天比你強大了...
(改)變(形)態篇 文章,僅供開拓思路,實際項目慎用!
全站熱搜