Home > Article > Backend Development > C# file operations
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; string[ ] args)
Open(@"D:wang.txt", FileMode.Append);//Open the file in Append mode (if it does not exist, it will be created)
byte[] info = { (byte)'h', (byte)'e ', (byte)'l', (byte)'l', (byte)'o' };//The information to be written
textFile.Write(info, 0, info.Length); // The Write method only Can write to byte arrays
FileStream newText = File. Create(@"D:newText.txt");//Create file
File
File.Delete(@"d:newText.txt"); Files with the same name)
//File.Copy(@"d:wang.txt", @"@"d:CopyWang.txt"); You can only move in the same disk. If the target path is incorrect, you cannot move
// file.move ( @"d: copywang.txt", @"d: amovewang.txt"); ////////////////// Set file attributes to read-only, hidden
//File.SetAttributes(@"D:copywang.txt", FileAttributes.ReadOnly | FileAttributes.Hidden); // At the same time, if you meet multiple attributes at the same time, you must use the place or (|).
////////////// determine whether the file exists
if (@"d: Copywang .txt"))//If it exists, even hidden files can be found
Displayed, as long as the Hidden attribute is not added
Console.WriteLine("File copywang.txt"); Console.WriteLine("File CopyWang.txt not found");
, the File class provides more support for Text text.
?AppendText: Append text to an existing file
?CreateText: Create or open a new file for writing text
?OpenText: Open an existing text file for reading
But the above method mainly operates on UTF-8 encoded text, which makes it inflexible. It is recommended that readers use the following code to operate txt files.
wealth in- in in-Texting in StreamReader textReader = new StreamReader(@"D:wang.txt", System.Text.Encoding.Default);//Open with default encoding File in String Str = TextReader.readtond (); // Read files
Console.writeline ("Read text content with streamReader:" + str);
textReader.close (); //////////// write content on txt files
streamWriter TextWriter = New StreamWriter (@"d: wang.txt"); I TextWriter.close ();
/*
System.io.directory class and system.directoryinfo class
mainly provides various operations about the directory. When used, you need to quote the system.io name space. Its main attributes and methods are introduced below through program examples.
////////////////////////Directory attribute setting method
DirectoryInfo dirInfo = new DirectoryInfo(@"D:wang1wang");//
.Hidden;// | FileAttributes.ReadOnly;//Set folder attributes
//////////////////The second parameter of the Delete method is of bool type, which can determine whether Delete non-empty directories.
// If the parameter value is true, the entire directory will be deleted, even if there are files or subdirectories in the directory; if it is false, the directory can only be deleted when it is empty.
//Directory.Delete(@"D:wang1", true); //If the file is set to ReadOnly, it cannot be deleted // Move the folder wang1 into the folder wang3, which is equivalent to deleting wang1, creating a wang3, and moving the content to wang3
string [] directories = directory.getDirectories (@"d: wang3");/ /Get the directory of the folder wang3
foreach (string var in Directories)
Console.WriteLine(var);
‐ ‐ ‐ ‐ ‐‐‐‐‐‐ All files
("Folder wang1 exists");
/*"" Is a special character in C#, and if it means it, you need to use "\". Since this way of writing is inconvenient, the C# language provides @ to simplify it. Just add @ in front of the string to use "" directly.在 Therefore, the above path should be expressed in C#as "book",@"tmpbook",@"c: tmpbook".
*/ Console.ReadLine();