Directory operations in C # (replication \ rename \ create \ Delete)

Source: Internet
Author: User
Tags foreach instance method

Directory classes are used for typical operations, such as copying, moving, renaming, creating, and deleting directories. You can also use the directory class to get and set DateTime information related to the creation, access, and write operations of the directory. Because all directory methods are static, the efficiency of using the directory method may be higher than using the corresponding DirectoryInfo instance method if you want to perform only one operation. Most directory methods require the path of the directory for the current operation.

Cases

The code is as follows Copy Code

1. Create, move, delete---------folders---------

Create a folder
Directory.CreateDirectory (Server.MapPath ("a"));
Directory.CreateDirectory (Server.MapPath ("B"));
Directory.CreateDirectory (Server.MapPath ("C"));
Move B to a
Directory.move (Server.MapPath ("B"), Server.MapPath ("a\b"));
Delete C
Directory.delete (Server.MapPath ("C"));

2. Create, copy, move, delete---------files---------

Create a file
When you create a copy/move/delete using File.create, you are prompted that the file is being used by another process, so the process cannot access the file
Use FileStream to get File.create returned System.IO.FileStream and then close it without this problem
FileStream FS;
FS = File.create (Server.MapPath ("a.txt"));
Fs. Close ();
FS = File.create (Server.MapPath ("B.txt"));
Fs. Close ();
FS = File.create (Server.MapPath ("C.txt"));
Fs. Close ();
Copying files
File.Copy (Server.MapPath ("A.txt"), Server.MapPath ("A\a.txt"));
Moving files
File.move (Server.MapPath ("B.txt"), Server.MapPath ("A\b.txt"));
File.move (Server.MapPath ("C.txt"), Server.MapPath ("A\c.txt"));
deleting files
File.delete (Server.MapPath ("a.txt"));

3.---------traverse the files and subfolders in the folder and display their properties---------

if (Directory.Exists (Server.MapPath ("a"))
{
All subfolders
foreach (String item in Directory.getdirectories (Server.MapPath ("a"))
{
Response.Write ("<b> folder:" + Item + "</b><br/>");
DirectoryInfo DirectoryInfo = new DirectoryInfo (item);
Response.Write ("Name:" + DirectoryInfo.) Name + "<br/>");
Response.Write ("Path:" + DirectoryInfo.) FullName + "<br/>");
Response.Write ("Create Time:" + DirectoryInfo.) CreationTime + "<br/>");
Response.Write (Last access time: + DirectoryInfo.) LastAccessTime + "<br/>");
Response.Write (Last Modified: "+ DirectoryInfo.") LastWriteTime + "<br/>");
Response.Write ("Parent folder:" + DirectoryInfo.) Parent + "<br/>");
Response.Write ("The root directory:" + DirectoryInfo.) Root + "<br/>");
Response.Write ("<br/>");
}

All child files
foreach (String item in Directory.GetFiles (Server.MapPath ("a"))
{
Response.Write ("<b> file:" + Item + "</b><br/>");
FileInfo FileInfo = new FileInfo (item);
Response.Write ("Name:" + FileInfo.) Name + "<br/>");
Response.Write ("extension:" + FileInfo.) Extension + "<br/>");
Response.Write ("Path:" + FileInfo.) FullName + "<br/>");
Response.Write ("Size:" + FileInfo.) Length + "<br/>");
Response.Write ("Create Time:" + FileInfo.) CreationTime + "<br/>");
Response.Write (Last access time: + FileInfo.) LastAccessTime + "<br/>");
Response.Write (Last Modified: "+ FileInfo.") LastWriteTime + "<br/>");
Response.Write ("folder:" + FileInfo.) DirectoryName + "<br/>");
Response.Write ("File properties:" + FileInfo.) Attributes + "<br/>");
Response.Write ("<br/>");
}
}

4.---------file reading and writing---------

if (File.exists (Server.MapPath ("A\a.txt"))
{
StreamWriter streamwrite = new StreamWriter (Server.MapPath ("A\a.txt"));
Streamwrite. WriteLine ("Wooden House");
Streamwrite. WriteLine ("http://www.111cn.net/");
Streamwrite. Write ("2008-04-13");
Streamwrite. Close ();

StreamReader StreamReader = new StreamReader (Server.MapPath ("A\a.txt"));
Response.Write (StreamReader. ReadLine ());
Response.Write (StreamReader. ReadToEnd ());
StreamReader. Close ();
}

Finally share a directory action class

The code is as follows Copy Code

<summary>
Directory Action Class
</summary>
public class Directoryhelper
{
Public Directoryhelper ()
{ }

<summary>
Get all disk drives
</summary>
<returns></returns>
Public string[] Alldrivers ()
{
return directory.getlogicaldrives ();
}

       ///<summary>
       / The current working directory of the application, Bin folder
       ///</summary>
        ///<returns></returns>
        Public String CurrentDirectory ()
        {
             return directory.getcurrentdirectory ();
       }

       ///<summary>
       / Get the files in the path directory, excluding folders
       ///</summary>
        ///<param name= "path" ></PARAM>
        ///<returns></returns>
        public string[] Allfiles ( String path)
        {
             return Directory.GetFiles (path);
       }

<summary>
Create a directory if the directory exists, then the directory is not created
</summary>
<param name= "Directory" ></param>
public void CreateDirectory (string directory)
{
DirectoryInfo info = new DirectoryInfo (directory);
Info. Create ();

DirectoryInfo info = directory.createdirectory (Directory);
}

<summary>
Delete Directory
</summary>
<param name= "Path" ></param>
public void DeleteDirectory (string path)
{
Directory.delete (path, false/* delete subdirectory */); The error occurs if the subdirectory is non-null.

Directory.delete (path, true/* keep subdirectory */); Ignore subdirectory contents, no content deleted
}

       ///<summary>
       / Directory of all files, hidden files are also visible, peeping?
       ///</summary>
        ///<param name= "path" ></PARAM>
       ///<returns ></returns>
        Public string[] allcontent (string path)
        {
             return directory.getfilesystementries (path);//Error
       } If path does not exist

<summary>
Copy file directory, copy complete original directory deletion
</summary>
<param name= "path1" ></param>
<param name= "path2" ></param>
public void CopyDirectory (string sourcepath/* must exist before replication is completed, */,string targetpath/* cannot exist before replication)
{
Directory.move (Sourcepath,targetpath);
}

<summary>
Code, to tell the truth I did not use, do not know why.
</summary>
<returns></returns>
public string codeing ()
{
Encoding code = Encoding.default;
byte[] bytes = code. GetBytes ("People's Republic of China");
byte[] bytes = code. GetBytes ("I ' m a smart boy");
Return encoding.getencoding ("UTF-8").       GetString (bytes); Does DOC environment not support Chinese? Chinese can not turn back normally, English is OK.
}
}

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.