"Go" C # add modify Delete Files folder Daquan

Source: Internet
Author: User
Tags create directory

"Go" C # add modify Delete Files folder Daquan

C # Add modify Delete Files folder Daquan

StreamWriter sw = File.appendtext (Server.MapPath (".") + " \\myText.txt ");
Sw. WriteLine ("Chasing the ideal");
SW. WriteLine ("kzlll");
SW. WriteLine (". NET Notes ");
SW. Flush ();
SW. Close ();
C # Copy file
string orignfile,newfile;
Orignfile = Server.MapPath (".") + "
\\myText.txt ";
NewFile = Server.MapPath (".") + "
\\myTextCopy.txt ";
File.Copy (orignfile,newfile,true);

C # Delete Files
String delfile = Server.MapPath (".") + "
\\myTextCopy.txt";
File.delete (Delfile);

C # moving files
String Orignfile,newfile;
Orignfile = Server.MapPath (".") + "
\\myText.txt";
NewFile = Server.MapPath (".") + "
\\myTextCopy.txt";
File.move (Orignfile,newfile);

C # Create directory
//Create directory C:\sixAge
DirectoryInfo d= Directory.CreateDirectory ("C:\\sixage");
//D1 point to C:\sixAge\sixAge1
DirectoryInfo d1=d.createsubdirectory ("SixAge1");
D2 points to c:\sixAge\sixAge1\sixAge1_1
DirectoryInfo d2=d1. Createsubdirectory ("Sixage1_1");
//Set the current directory to C:\sixAge
Directory.setcurrentdirectory ("C:\\sixage");
Create the directory C:\sixAge\sixAge2
Directory.CreateDirectory ("SixAge2");
Create directory C:\sixAge\sixAge2\sixAge2_1
directory.createdirectory ("Sixage2\\sixage2_1");

Recursively delete folders and files
<%@ Page language=c#%>
<%@ Import namespace= "System.IO"%>
<script runat=server>
public void DeleteFolder (string dir)
{
if (Directory.Exists (dir))//If there is a deletion of this folder
{
foreach (String D in Directory.getfilesystementries (dir))
{
if (file.exists (d))
File.delete (d); Delete files directly from them
Else
DeleteFolder (d); Recursively Delete Subfolders
}
Directory.delete (dir); Delete an empty folder
Response.Write (dir+ "folder deletion succeeded");
}
Else
Response.Write (dir+ "This folder does not exist"); Prompt if the folder does not exist
}

protected void Page_Load (Object sender, EventArgs e)
{
String dir= "d:\\gbook\\11";
DeleteFolder (Dir); Calling a function to delete a folder
}


// ======================================================
Implement a static method copy all the contents below the specified folder to the target folder
If the destination folder is a read-only property, an error occurs.
April 18april2005 in STU
// ======================================================
public static void Copydir (String srcpath,string aimpath)
{
Try
{
Check whether the destination directory ends with a directory split character if it is not added
if (aimpath[aimpath.length-1]! = Path.directoryseparatorchar)
Aimpath + = Path.directoryseparatorchar;
Determine if the target directory exists and if it does not exist, create a new
if (! Directory.Exists (Aimpath)) directory.createdirectory (Aimpath);
Get a list of the files in the source directory, which is an array containing the files and directory paths
If you point to the file below the copy destination file and do not include the directory, use the following method
string[] fileList = Directory.GetFiles (Srcpath);
string[] fileList = directory.getfilesystementries (Srcpath);
Traverse All files and directories
foreach (string file in FileList)
{
As directory processing, if this directory is present, recursively copy the files under the directory
if (directory.exists (file))
Copydir (file,aimpath+path.getfilename (file));
Otherwise, the direct copy file
Else
File.Copy (file,aimpath+path.getfilename (file), true);
}
}
catch (Exception e)
{
MessageBox.Show (E.tostring ());
}
}

// ======================================================
Implement a static method that will specify all the contents of the folder below Detele
Be careful when testing, and cannot be recovered after deletion.
April 18april2005 in STU
// ======================================================
public static void Deletedir (String aimpath)
{
Try
{
Check whether the destination directory ends with a directory split character if it is not added
if (aimpath[aimpath.length-1]! = Path.directoryseparatorchar)
Aimpath + = Path.directoryseparatorchar;
Get a list of the files in the source directory, which is an array containing the files and directory paths
If you point to the file below the delete target file and do not include the directory, use the following method
string[] fileList = Directory.GetFiles (Aimpath);
string[] fileList = directory.getfilesystementries (Aimpath);
Traverse All files and directories
foreach (string file in FileList)
{
As directory processing, if the directory is present, recursively delete the file under the directory.
if (directory.exists (file))
{
Deletedir (aimpath+path.getfilename (file));
}
Otherwise, delete the file directly
Else
{
File.delete (aimpath+path.getfilename (file));
}
}
Delete a folder
System.IO. Directory. Delete (aimpath,true);
}
catch (Exception e)
{
MessageBox.Show (E.tostring ());
}
}

You need to reference a namespace:
Using System.IO;

<summary>
Copy folders (including subfolders) to the specified folder, both the source folder and the destination folder require an absolute path. Format: CopyFolder (source folder,

Target folder);
</summary>
<param name= "Strfrompath" ></param>
<param name= "Strtopath" ></param>

//--------------------------------------------------
Author: Go to Beggar qq:305725744 tomorrow
//---------------------------------------------------

public static void CopyFolder (String strfrompath,string strtopath)
{
If the source folder does not exist, create
if (! Directory.Exists (Strfrompath))
{
Directory.CreateDirectory (Strfrompath);
}

Get the folder name you want to copy
String strfoldername = Strfrompath.substring (strfrompath.lastindexof ("\ \") + 1,strfrompath.length

-Strfrompath.lastindexof ("\ \")-1);

Create a source folder in the destination folder if there is no source folder in the destination folder
if (! Directory.Exists (Strtopath + "\" + strfoldername)
{
Directory.CreateDirectory (Strtopath + "\" + strfoldername);
}
Create an array save the file name under the source folder
string[] Strfiles = Directory.GetFiles (Strfrompath);

Looping copy files
for (int i = 0;i < strfiles.length;i++)
{
Get the file name of the copy, only take the file name, the address is cut off.
String strFileName = Strfiles[i]. Substring (Strfiles[i]. LastIndexOf ("\ \") + 1,strfiles[i]. Length-

Strfiles[i]. LastIndexOf ("\ \")-1);
Start copying files, true means overwriting files with the same name
File.Copy (Strfiles[i],strtopath + "\" + strfoldername + "\ \" + strfilename,true);
}

Creating an DirectoryInfo instance
DirectoryInfo dirinfo = new DirectoryInfo (Strfrompath);
Get all subfolder names under the source folder
directoryinfo[] Zipath = Dirinfo.getdirectories ();
for (int j = 0;j < zipath.length;j++)
{
Get all subfolder names
String Strzipath = Strfrompath + "\ \" + zipath[j]. ToString ();
Take the resulting subfolder as a new source folder and start a new copy from scratch
CopyFolder (Strzipath,strtopath + "\" + strfoldername);
}
}

A Reading text files
1///<summary>
2///reading a text file
3///</summary>
4private void Readfromtxtfile ()
5{
6 if (FilePath.PostedFile.FileName! = "")
7 {
8 Txtfilepath =filepath.postedfile.filename;
9 Fileextname = txtfilepath.substring (Txtfilepath.lastindexof (".") +1,3);
10
One if (fileextname! = "txt" && fileextname! = "txt")
12 {
Response.Write ("Please select text file");
14}
All else
16 {
StreamReader FileStream = new StreamReader (Txtfilepath,encoding.default);
Txtcontent.text = Filestream.readtoend ();
Filestream.close ();
20}
21}
22}
Two Get a list of files
1///<summary>
2///getting a list of files
3///</summary>
4private void Getfilelist ()
5{
6 string Strcurdir,filename,fileext;
7
8///File size
9 Long FileSize;
10
11///Last modification time;
DateTime filemodify;
13
14//Initialization
if (! IsPostBack)
16 {
17///Initialize, default to the directory where the current page is located
Strcurdir = Server.MapPath (".");
Lblcurdir.text = Strcurdir;
Txtcurdir.text = Strcurdir;
21}
All else
23 {
Strcurdir = Txtcurdir.text;
Txtcurdir.text = Strcurdir;
Lblcurdir.text = Strcurdir;
27}
FileInfo fi;
DirectoryInfo dir;
TableCell TD;
TableRow TR;
TR = new TableRow ();
33
34///Dynamic adding cell contents
td = New TableCell ();
TD. Controls.Add (New LiteralControl ("filename"));
Panax Notoginseng tr. Cells.add (TD);
TableCell td = New ();
The TD. Controls.Add (New LiteralControl ("file type"));
Max tr. Cells.add (TD);
* td = New TableCell ();
TD. Controls.Add (New LiteralControl ("File Size"));
Tr. Cells.add (TD);
td = New TableCell ();
TD. Controls.Add (New LiteralControl ("Last Modified Time"));
Tr. Cells.add (TD);
47
TABLEDIRINFO.ROWS.ADD (TR);
49
50///Create a directory Reference object for the current directory
Wuyi DirectoryInfo dirinfo = new DirectoryInfo (txtcurdir.text);
52
53///cycle to determine the files and directories in the current directory
Si-foreach (FileSystemInfo FSI in Dirinfo.getfilesysteminfos ())
55 {
* * * FileName = "";
Fileext = "";
FileSize = 0;
59
60//If it is a file
If (FSI is FileInfo)
62 {
N-fi = (FileInfo) FSI;
64
65///Get file name
The FileName = fi. Name;
67
68///Get the file extension
Fileext = fi. Extension;
70
71///Get the size of the file
FileSize = fi. Length;
73
74///obtained last modified time for file
Filemodify = fi. LastWriteTime;
76}
77
78///Otherwise the directory
+ Else
80 {
Bayi dir = (DirectoryInfo) FSI;
82
83///Get directory Name
The FileName = dir. Name;
85
86///Get the last modified time for the directory
Filemodify = dir. LastWriteTime;
88
89///settings file has a filename extension of "folder"
Fileext = "folder";
91}
92
93///Dynamic Add Table contents
94 TR = new TableRow ();
td = New TableCell ();
TD. Controls.Add (New LiteralControl (FileName));
The tr. Cells.add (TD);
98 TD = New TableCell ();
TD. Controls.Add (New LiteralControl (Fileext));
Tr. Cells.add (TD);
101 td = New TableCell ();
102 TD. Controls.Add (New LiteralControl (filesize.tostring () + "byte"));
103 tr. Cells.add (TD);
104 td = New TableCell ();
The TD. Controls.Add (New LiteralControl (filemodify.tostring ("Yyyy-mm-dd hh:mm:ss"));
106 tr. Cells.add (TD);
107 TableDirInfo.Rows.Add (TR);
108}
109}

Three Read log file

1///<summary>
2///read log file
3///</summary>
4private void Readlogfile ()
5{
6//reads the log file from the specified directory in the form of open or created
7 FileStream fs = new FileStream (Server.MapPath (" Upedfile ") +"
\\logfile.txt Span style= "COLOR: #134f5c" " ",

FileMode.OpenOrCreate, FileAccess.Read);
8
9///define output string
StringBuilder output = new StringBuilder ();
11
12///Initialize the string with a length of 0
Output. Length = 0;
14
15///create read data stream for the file stream created above
StreamReader read = new StreamReader (FS);
17
18///Set the starting position of the current stream as the starting point for the file stream
Read. Basestream.seek (0, Seekorigin.begin);
20
21///Read file
while (read. Peek () >-1)
23 {
24///Take a line of the file and wrap it
Output. Append (read. ReadLine () + "\ n");
26}
27
28///off-release read data stream
Read. Close ();
30
31///Returns the contents of the log file read
return output. ToString ();
33}

"Go" C # add modify Delete Files folder Daquan

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.