C # File operations

Source: Internet
Author: User
Tags create directory
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.IO;

Namespace Example
{

Class Program
{

static void Main (string[] args)
{
File opens the following code to open D:\wang.txt file, and writes "Hello" to the file
FileStream textfile = File.Open (@ "D:\wang.txt", filemode.append);//Open file in Append mode (if not present, create)
Byte[] info = {(byte) ' H ', (byte) ' E ', (byte) ' L ', (Byte) ' L ', (byte) ' O '};//information to write
Textfile.write (info, 0, info.) Length);//write method can only write to a byte array
Textfile.close ();//Close file stream


File creation
FileStream NewText = file.create (@ "D:\newText.txt");//Create File
Newtext.close ();//Close file

deleting files
File.delete (@ "D:\newText.txt");

File replication If the destination file exists, it is not allowed to replicate (that is, files with the same name cannot be overwritten)
File.Copy (@ "D:\wang.txt", @ "D:\CopyWang.txt");


File movement can only be moved in the same disk if the destination path is not correct, you cannot move
File.move (@ "D:\CopyWang.txt", @ "D:\A\movewang.txt");

Set file properties to read-only, hide
File.setattributes (@ "D:\copywang.txt", Fileattributes.readonly | Fileattributes.hidden);//satisfy multiple attributes at the same time, you must use a bit or (|).

Judging if the file is not present
if (File.exists (@ "D:\copywang.txt"))//If there are even hidden files can be found
{
File.setattributes (@ "D:\copywang.txt", fileattributes.readonly);//After you reset the properties, the hidden files are also displayed, as long as you do not add the hidden property
Console.WriteLine ("Find File Copywang.txt");
}
Else
{
Console.WriteLine ("File CopyWang.txt not found");
}
/*
In addition, 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
However, the above methods are mainly used to manipulate the encoded text of UTF-8, and thus appear not flexible enough. It is recommended that the reader use the following code to manipulate the TXT file.
"read" The TXT file, the sample code is as follows:
*/
StreamReader TextReader = new StreamReader (@ "D:\wang.txt", System.Text.Encoding.Default);//Open file as default encoding
String str = Textreader.readtoend ();//Read file
Console.WriteLine ("Read text content using StreamReader:" + str);
Textreader.close ();

Write content to TXT file
StreamWriter textWriter = new StreamWriter (@ "D:\wang.txt");
str = "learn. Net";
Textwriter.write (str);
Textwriter.close ();

/*
System.IO.Directory class and System.directoryinfo class
It mainly provides various operations about the directory, which need to refer to the System.IO namespace. The main properties and methods are described below through a program instance.
*/
Directory.CreateDirectory (@ "D:\wang1\wang");//Create directory (folder) if it already exists, persist; You can also create a multilevel directory at a time

Directory property setting method
DirectoryInfo dirinfo = new DirectoryInfo (@ "D:\wang1\wang");//
Dirinfo.attributes = fileattributes.hidden;//| fileattributes.readonly;//Setting folder properties

The second parameter of the Delete method is type bool, which can decide whether to delete a non-empty directory.
If the parameter value is true, the entire directory is deleted, even if there are files or subdirectories under that directory, or False if the directory is empty.
Directory.delete (@ "D:\wang1", true);//If the file is set to ReadOnly, you cannot delete

Directory.move (@ "d:\wang1", @ "D:\wang3");//Move the folder Wang1 to the folder Wang3, equivalent to delete wang1, create a wang3, and then move the content to Wang3

string[] directories = directory.getdirectories (@ "D:\wang3");//Get Folder Wang3 directory
foreach (string var in directories)
Console.WriteLine (Var);

string[] files = Directory.GetFiles (@ "D:\wang1");//Get folder wang1 all files below
foreach (string var in Files)
Console.WriteLine (Var);

if (Directory.Exists (@ "D:\wang1"))
Console.WriteLine ("Folder wang1 exists");

/*
In C #, "\" is a special character and you need to use "\ \" to represent it. Because of this inconvenient wording, the C # language provides a simplified @. You can use "\" directly before the string by adding @.
So the above path should be represented as "book" in C #, @ "\tmp\book", @ "C:\Tmp\Book".
*/

Console.ReadLine ();

}
}
}
  • 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.