C # Revolution in the string extension method

Source: Internet
Author: User

String is the most basic data type in a variety of programming languages. It has been overwhelmed by other classes for a long time and is often disassociated (Substring, Split), torn (Join )...

Now the string is about to turn over and make a revolution. It is almost omnipotent. You can do whatever you want to, and other types of fear...

 

Let's take a look at what string has done after the revolution?

 

 1. Open a file or website

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

How can we do this? Look at the extension. It is very simple to directly call the Process. Start function:

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

Only opening a file and stealing others' information is a preliminary operation. Strings can also be modified, deleted, or created (or directory)

 

2. file and directory operations

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

The implementation is as simple as calling the File and Directory classes. The implementation of the above three extensions is as follows. (Of course, more file and directory operations can be implemented, which is very simple and will not be provided !)

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}

I still feel uncomfortable. It is too troublesome to use one of the above files to delete the entire hard disk. No problem. See the following:

 

 3. Execute the doscommandFirst, let's look at two simple

1 string output1 = "del c: \ t1.txt"
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.