In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is an example analysis of the application of text files operated by C#. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
Application example of C # operating text file:
Using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Text / / < summary > C # operation text file application example / C # operation text file class / program (website) directory: d:\ Test / operation text file: d:\ Test\ file / / < / summary > public partial class _ Default: System.Web.UI.Page {/ / garbled when reading Chinese in the txt file / / solution: StreamReader sr = new StreamReader (fileName,Encoding.GetEncoding ("gb2312")) Protected void Page_Load (object sender, EventArgs e) {# region C# reads the text file (garbled resolved) {string fileName = Server.MapPath (@ "~\ file") + @ "\ read.txt"; StreamReader sr = new StreamReader (fileName, Encoding.GetEncoding ("gb2312")); / / reads the text in gb2312 character encoding format. String str; string result = ""; while ((str = sr.ReadLine ())! = null) / / read every line {result + = str;} sr.Close (); sr.Dispose ();} # endregion # region C# to write text file C# operation text file application instance {/ / string path = Server.MapPath (@ ".\ file"); / / these two sentences are equivalent. / / string path3 = Server.MapPath (@ "~\ file"); / / CreateText (): / / create or open a file to write UTF-8-encoded text. StreamWriter rw = File.CreateText (Server.MapPath (@ ".\ file") + @ "\ write.txt"); rw.WriteLine ("Hello"); / / write three lines of data. Rw.WriteLine ("hello"); rw.WriteLine ("China"); rw.Flush (); rw.Close (); rw.Dispose ();} # endregion # region opens a text file for reading. (read Chinese garbled) {/ / C # manipulate text file application example / / OpenText (): open an existing UTF-8-encoded text file for reading. StreamReader sr = File.OpenText (Server.MapPath (@ ".\ file") + @ "\ open.txt"); StringBuilder output = new StringBuilder (); string str; while ((str = sr.ReadLine ())! = null) {output.Append (str + "+");} string result = output.ToString (); sr.Close (); sr.Dispose () } # endregion # region C# append text to an existing file {/ / C # manipulate a text file application instance / / File.AppendText (): / / create a StreamWriter that appends UTF-8-encoded text to an existing file. StreamWriter sw = File.AppendText (Server.MapPath (@ ".\ file") + @ "\ append.txt"); sw.WriteLine ("Welcome"); sw.WriteLine ("come"); sw.WriteLine ("China"); sw.Flush (); sw.Close (); sw.Dispose ();} # endregion # region C# copy file {string from, to; from = Server.MapPath (@ ".\ file") + @ "\ copyFrom.txt" To = Server.MapPath (@ ".\ file") + @ "\ copyTo.txt"; File.Copy (from, to, true); / / true/false: whether overwriting the target file is allowed. If the target file does not exist, it is created automatically. } # endregion # region C# Delete file {string delFile = Server.MapPath (@ ".\ file") + @ "\ delFile.txt"; / / path to delete file File.Delete (delFile);} # endregion # region C# mobile file {/ / string From, To; / / From = Server.MapPath (".") + @ "\ MoveFrom.txt"; / / To = Server.MapPath (@ ".\ file") + @ "\ MoveFromTo.txt" / / File.Move (From, To); / / move and rename} # endregion # region C# create directory / / Directory-DirectoryInfo {DirectoryInfo d = Directory.CreateDirectory (Server.MapPath (@ ".\ file") + @ "\ CreateDirectory"); / / create subdirectory DirectoryInfo D1 = d.CreateSubdirectory ("CreateDirectory1"); DirectoryInfo D2 = d1.CreateSubdirectory ("CreateDirectory2") / / the current working directory of the application: / / D:\ Program Files\ Microsoft Visual Studio 8\ Common7\ IDE string cur = Directory.GetCurrentDirectory (); / / set the current directory to Server.MapPath (@ ".\ file") Directory.SetCurrentDirectory (Server.MapPath (@ ".\ file")); / / (in the current working directory) create a directory DirectoryInfo D3 = Directory.CreateDirectory ("sixAge2") / / create directory C # operation text file application instance DirectoryInfo d4 = Directory.CreateDirectory (@ "sixAge2\ sixAge2_1"); / / the current working directory of the application string cur1 = Directory.GetCurrentDirectory ();} # endregion}}
Note: create a website with the name Test under the root directory of the D disk.
The basic content of the application example of C # operating text file will be introduced to you here.
The above is an example analysis of the application of text files operated by C#. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.