string是各种编程语言中最基础的数据类型,长期以来受尽其它类的压迫,经常被肢解(Substring、Split)、蹂躏(Join)...

 而现在string要“翻身闹革命”了,它几乎无所不能,可以为所欲为,令其它类心惊胆颤...

 

 让我们来看一下革命后的string做了些什么?

 1. 打开文件或网址


1      "c:\\t.txt".Open();

 

 怎么做到的呢?看扩展,很简单,直接调用调用了Process.Start函数:


1     public static void Open(this string s)

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();


 实现同样简单,调用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     }


  还是感觉不过瘾,想要删除整个硬盘的文件,用上面的一个一个来也太麻烦了。也没问题,看下面:

 3. 执行DOS命令,先看两个简单的 


1     string output1 = "del c:\\t1.txt".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     }


 DOS命令也会有异常发生,下面的实现可通过out参数返回错误信息:


艾伟_转载:c#扩展方法奇思妙用变态篇四:string 的翻身革命_编程语言艾伟_转载:c#扩展方法奇思妙用变态篇四:string 的翻身革命_编程语言_02ExecuteDOS

 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     }


 有了这个扩展,格式化硬盘、关机、重启都不在话下!


1     "format c:".ExecuteDOS();

2     "shutdown -s".ExecuteDOS();

3     "shutdown -r".ExecuteDOS();


 以上对付一般用户的电脑足够了,可但对程序员的电脑,他们居然把信息放进了数据库!同样有办法!

 4. 执行SQL


1     DbConnection conn = 艾伟_转载:c#扩展方法奇思妙用变态篇四:string 的翻身革命_目录操作_03

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     }


 还有Cast扩展:


1     public static T Cast<T>(this object obj)

2     {

3         return (T)obj;

4     }


 现在可以执行了。结果是***  同样还可以实现更多数据库操作。

 string还可以做更多更多事情,只要你支持它!但不要给它太多太大的权力,万一哪天比你强大了...