Common C file operations

 

C # append files
StreamWriter sw = File.AppendText (Server.MapPath (“.”) + “/ MyText.txt”);
sw.WriteLine (“test”);
sw.WriteLine (“Rochael Zhang”);
sw.WriteLine (CSharp learning. “);
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 the file
string delFile = Server.MapPath (“.”) + “/ myTextCopy.txt”;
File.Delete (delfile);

C # move files
string OrignFile, NewFile;
OrignFile = Server.MapPath (“.”) + “/ MyText.txt”;
NewFile = Server.MapPath (“.”) + “/ MyTextCopy.txt”;
File.Move (OrignFile, NewFile);

C # to create directory
/ / Create the directory c: sixAge
DirectoryInfo d = Directory.CreateDirectory (“c :/ sixAge”);
/ / D1 pointing to the c: sixAgesixAge1
DirectoryInfo d1 = d.CreateSubdirectory (“sixAge1”);
/ / D2 point to c: sixAgesixAge1sixAge1_1
The DirectoryInfo from d2 = d1.CreateSubdirectory (sixAge1_1);
/ / Current directory to the c: sixAge
Directory.SetCurrentDirectory (“c :/ sixAge”);
/ / Create the directory c: sixAgesixAge2
Directory.CreateDirectory (“sixAge2”);
/ / Create the directory c: sixAgesixAge2sixAge2_1
Directory.CreateDirectory (“sixAge2/sixAge2_1”);

The recursive delete the folders and files
<% @ Page Language = C #%>
<% @ Import namespace = “System.IO”%>
<Script runat = server>
public   the void The DeleteFolder (String dir)
{
the the if (Directory.Exists (dir)) / / If there is delete this folder
{
foreach (string for d in Directory.GetFileSystemEntries (dir))
{
if (File.Exists (d))
The File.Delete (d); / / delete the file
else
The The DeleteFolder (d); / / recursive delete subfolders
}
Directory.Delete (dir); / / delete empty folders
Response.Write (dir + “folder deleted successfully”);
}
else
The Response.Write (dir + “the folder does not exist”); / / If the folder does not exist prompt
}

protected   void Page_Load (Object sender, EventArgs e)
{
string Dir = “D :/ gbook/11”;
The DeleteFolder (Dir); / / call the function to delete the folder
}

/ / ================================================ ======
/ / A static method of the specified folder below the following copy the contents to the destination folder
/ / If the destination folder is read-only attribute an error.
/ / April 18April2005 In STU
/ / ================================================ ======
public   static   void CopyDir (string srcPath, string aimPath)
{
try
{
/ / Check whether the target directory to directory partition the end of the character if not add
if (aimPath [aimPath.Length – 1]! = Path.DirectorySeparatorChar)
aimPath + = Path.DirectorySeparatorChar;
/ / Determine the target directory exists if it does not exist, create the new
if (! Directory.Exists (aimPath)) Directory.CreateDirectory (aimPath);
/ / Get a list of files of the source directory, which contains an array of file and directory path
/ / If you point to copy the target file does not contain the following files directory, please use the following method
/ / String [] filelist = Directory.GetFiles (srcpath,);
string [] fileList = Directory.GetFileSystemEntries (srcPath);
/ / Loop through all the files and directories
foreach (string file in FileList)
{
/ / As the directory processing recursive Copy the directory below this directory file
the if (Directory.Exists (file))
CopyDir (file, aimPath + Path.GetFileName (file));
/ / Otherwise Copy files directly
else
File.Copy (file, aimPath + Path.GetFileName (file), true);
}
}
catch (Exception e)
{
MessageBox.Show (e.ToString ());
}
}

/ / ================================================ ======
/ / A static method specified folder all Detele below
/ / Test to be careful when operating, can not be recovered after deletion.
/ / April 18April2005 In STU
/ / ================================================ ======
public   static   void DeleteDir (string aimPath)
{
try
{
/ / Check whether the target directory to directory partition the end of the character if not add
if (aimPath [aimPath.Length – 1]! = Path.DirectorySeparatorChar)
aimPath + = Path.DirectorySeparatorChar;
/ / Get a list of files of the source directory, which contains an array of file and directory path
/ / If you point to Delete the target file does not contain the following files directory, please use the following method
/ / String [] filelist = Directory.GetFiles (aimPath,);
string [] fileList = Directory.GetFileSystemEntries (aimPath);
/ / Loop through all the files and directories
foreach (string file in FileList)
{
/ / As the directory processing recursive Delete the directory below this directory file
the if (Directory.Exists (file))
{
DeleteDir (aimPath + Path.GetFileName (file));
}
/ / Otherwise Delete files directly
else
{
File.Delete (aimPath + Path.GetFileName (file));
}
}
/ / Delete the folder
System.IO. Directory. Delete (aimPath, true);
}
catch (Exception e)
{
MessageBox.Show (e.ToString ());
}
}

Need to reference the namespace:
using System.IO;

/ ** / / / /   <summary>
/ / / Copy the folder (including subfolders) to the specified folder, the source folder and destination folder need to be an absolute path format: the the CopyFolder (source folder to the destination folder);
/ / /   </ Summary>
/ / /   <param name=”strFromPath”> </ param>
/ / /   <param name=”strToPath”> </ param>

/ / ———————————————— –
/ / Author: tomorrow go begging QQ: 305 725 744
/ / ———————————————— —

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 files you want to copy the folder name
string strFolderName = strFromPath.Substring (strFromPath.LastIndexOf (“/”) +   1, strFromPath.Length – strFromPath.LastIndexOf (“/”) –   1);

/ / If the destination folder in the source folder in the source folder is created in the destination folder
if (! Directory.Exists (strToPath +   “/”   + StrFolderName))
{
Directory.CreateDirectory (strToPath +   “/”   + StrFolderName);
}
/ / Create array containing the source folder under the file name
string [] strFiles = Directory.GetFiles (strFromPath);

/ / Loop to copy files
for (int I =   0; i <strFiles.Length of; i + +)
{
/ / Get a copy of the file name, only the file name, the address is truncated.
string strFileName = strFiles [i]. the Substring (strFiles [i]. lastIndexOf (“/”) +   1, strFiles [i] the Length – strFiles [i]. LastIndexOf (“/”) –   1);
/ / Start copying files, true to overwrite the same name file
File.Copy (strFiles [i], strToPath +   “/”   + StrFolderName +   “/”   + StrFileName true);
}

/ / Create a DirectoryInfo instance
DirectoryInfo the DirInfo is =   new DirectoryInfo (strFromPath);
/ / Get the source folder under the subfolder name
DirectoryInfo [] ZiPath = dirInfo.GetDirectories ();
for (int j =   0; the j <ZiPath.Length; j + +)
{
/ / Get all the subfolder name
string strZiPath = strFromPath +   “/”   + ZiPath [j]. ToString ();
/ / Sub file folder as a new source folder, from the beginning of a new round of copy
CopyFolder (strZiPath, strToPath +   “/”   + StrFolderName);
}
}

A. Read a text file
/ ** / / / /   <summary>
/ / / Read a text file
/ / /   </ Summary>
private   void ReadFromTxtFile ()
{
the if (filePath.PostedFile.FileName! =   “”)
{
txtFilePath = filePath.PostedFile.FileName;
fileExtName = txtFilePath.Substring (txtFilePath.LastIndexOf (“.”) + 1, 3);

if (fileExtName! = “txt”   The && fileExtName! =   “TXT”)
{
Response.Write (“Please select a text file”);
}
else
{
StreamReader fileStream =   new StreamReader (txtFilePath, Encoding.Default);
txtContent.Text = fileStream.ReadToEnd ();
fileStream.Close ();
}
}
}

Aly Chiman

Aly Chiman is a Blogger & Reporter at AlyChiTech.com which covers a wide variety of topics from local news from digital world fashion and beauty . AlyChiTech covers the top notch content from the around the world covering a wide variety of topics. Aly is currently studying BS Mass Communication at University.