zoukankan      html  css  js  c++  java
  • c# 扩展方法奇思妙用变态篇四:string 的翻身革命

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

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

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

    打开文件或网址

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

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

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

     单单打开个文件,窃取他人信息只是初步操作,string还可以修改、删除、创建文件(或目录)

    文件及目录操作

    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     }
    复制代码

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

    执行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参数返回错误信息:


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

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

    执行SQL

     

    1     DbConnection conn = 
    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还可以做更多更多事情,只要你支持它!但不要给它太多太大的权力,万一哪天比你强大了...

     (改)变(形)态篇 文章,仅供开拓思路,实际项目慎用!

  • 相关阅读:
    正则表达式
    CSS常用格式
    数据库原理第十章考试形式
    汇编语言(王爽第三版) 实验5编写、调试具体多个段的程序
    7-10 多项式A除以B (25分)(多项式除法)
    C. Air Conditioner(区间交集)
    X的因子链(分解质因数,)
    AtCoder Beginner Contest 155 E.Payment
    cf584DDima and Lisa(素数性质,三素数,哥德巴赫猜想)
    CF1207C Gas Pipeline(DP)
  • 原文地址:https://www.cnblogs.com/ywsoftware/p/3128816.html
Copyright © 2011-2022 走看看