Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of the Application of C # operating text File

Shulou Source: shulou.com Published: 2022-06-02 18:58:07 09月23日 Update

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.

Tags: Files text caching applications directories examples coding work garbled programs UTF-8 examples Analysis applications more goals knowledge articles websites China Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL OPPO Reno Microsoft Xiaomi macOS