Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the data type string in C #

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use the data type string in C#". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use the data type string in C#".

String is the most basic data type in various programming languages, but now the C# data type string is about to make a revolution. It is almost omnipotent, can do whatever it wants, and makes other classes tremble.

Let's take a look at what string did after the revolution.

1. Open a file or URL

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

How do you do that? Look at the extension, it is very simple, directly call the Process.Start function:

Public static void Open (this string s) {Process.Start (s);}

Simply opening a file and stealing other people's information is only a preliminary operation. String can also modify, delete, and create files (or directories).

two。 File and directory operation

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

The implementation is just as simple, calling the File and Directory classes. The following are the implementation of the above three extensions. (of course, you can also achieve more file and directory operations, very simple, no longer given! )

Public static void CreateDirectory (this string path) {Directory.CreateDirectory (path);} public static void WriteText (this string path, string contents) {File.WriteAllText (path, contents);} public static void DeleteFile (this string path) {if (File.Exists (path)) File.Delete (path);}

It still doesn't feel good. It's too troublesome to use one of the above to delete the files on the entire hard drive. No problem, look at the following:

3. To execute the DOS command, first look at two simple

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

The Process class is also used in the implementation, as follows:

Public static string ExecuteDOS (this string cmd) {Process process = new Process (); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; process.Start (); process.StandardInput.WriteLine (cmd); process.StandardInput.WriteLine ("exit") Return process.StandardOutput.ReadToEnd ()

Exceptions can also occur in the DOS command. The following implementation returns an error message through the out parameter:

ExecuteDOS

Public static string ExecuteDOS (this string cmd, out string error) {Process process = new Process (); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; process.Start (); process.StandardInput.WriteLine (cmd); process.StandardInput.WriteLine ("exit") Error = process.StandardError.ReadToEnd (); return process.StandardOutput.ReadToEnd ();}

With this extension, formatting the hard drive, shutting down, and rebooting are easy!

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

The above is enough for the average user's computer, but for the programmer's computer, they put the information into the database! There is also a way!

4. Execute SQL

DbConnection conn = int count = "select count (*) from Girlfriends" .ExecuteScalar (conn) .Cast

< int>

()

The reference implementation is as follows:

Public static object ExecuteScalar (this string sql, DbConnection conn) {object result; using (DbCommand cmd = conn.CreateCommand ()) {cmd.Connection = conn; cmd.CommandText = sql; cmd.CommandType = System.Data.CommandType.Text; conn.Open (); result = cmd.ExecuteScalar (); conn.Close ();} return result;}

There are also Cast extensions:

Public static T Cast

< T>

(this object obj) {return (T) obj;}

It can be executed now. The result is that * * can also implement more database operations.

The above is all the contents of the article "how to use data types string in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report