C # File Operation classes

Source: Internet
Author: User

Using System;
Using System.IO;

Namespace Utils
{
public class Iohelper
{
public Iohelper ();

public static bool Copydir (DirectoryInfo Fromdir, string todir); Copy Directory
public static bool Copydir (string fromdir, string todir); Copy Directory
public static bool Createdir (string dirName); Create a Directory
public static bool CreateFile (string fileName); Create a file
public static void Deletedir (DirectoryInfo dir); Delete directory (delete if file exists in directory)
public static bool Deletedir (string dir, bool Onlydir); Delete Directory
public static bool DeleteFile (string fileName);//delete file
public static bool Exists (string fileName);//Determine if the file exists
public static bool FindFile (DirectoryInfo dir, string fileName);//Find a file in the specified directory
public static bool FindFile (String dir, string fileName);//Locate the file in the specified directory
public static string read (string fileName);//Read the entire contents of the file
public static string ReadLine (string fileName);//Read first row of data
public static bool Write (string fileName, string content);//writes the specified contents

public static bool WriteLine (string fileName, string content);//write a row of data
}
}

Using System;
Using System.Text;
Using System.IO;
/*----------------------------------------------------------------
File name: Iohelper
File function Description: File Operation class
//
Person created: Chen Taihan
Date Created: 2011/05/18

----------------------------------------------------------------*/
Namespace Utils
{
public class Iohelper
{
<summary>
Determine if a file exists
</summary>
<param name= "FileName" ></param>
<returns></returns>
public static bool Exists (string fileName)
{
if (FileName = = NULL | | Filename.trim () = = "")
{
return false;
}

if (file.exists (fileName))
{
return true;
}

return false;
}


<summary>
Create a folder
</summary>
<param name= "DirName" ></param>
<returns></returns>
public static bool Createdir (string dirName)
{
if (! Directory.Exists (DirName))
{
Directory.CreateDirectory (DirName);
}
return true;
}


<summary>
Create a file
</summary>
<param name= "FileName" ></param>
<returns></returns>
public static bool CreateFile (string fileName)
{
if (! File.exists (FileName))
{
FileStream fs = File.create (FileName);
Fs. Close ();
Fs. Dispose ();
}
return true;

}


<summary>
Read File contents
</summary>
<param name= "FileName" ></param>
<returns></returns>
public static string Read (String fileName)
{
if (! Exists (FileName))
{
return null;
}
Read file information into the stream
using (FileStream fs = new FileStream (FileName, FileMode.Open))
{
return new StreamReader (FS). ReadToEnd ();
}
}


public static string ReadLine (String fileName)
{
if (! Exists (FileName))
{
return null;
}
using (FileStream fs = new FileStream (FileName, FileMode.Open))
{
return new StreamReader (FS). ReadLine ();
}
}

<summary>
Write a file
</summary>
<param name= "filename" > file name </param>
<param name= "Content" > File contents </param>
<returns></returns>
public static bool Write (string fileName, string content)
{
if (! Exists (fileName) | | Content = = null)
{
return false;
}

Read file information into the stream
using (FileStream fs = new FileStream (FileName, FileMode.OpenOrCreate))
{
Lock (FS)//latched stream
{
if (!fs. CanWrite)
{
throw new System.Security.SecurityException ("File filename=" + FileName + "is a read-only file cannot be written!");
}

byte[] buffer = Encoding.Default.GetBytes (content);
Fs. Write (buffer, 0, buffer. Length);
return true;
}
}
}


<summary>
Write a row
</summary>
<param name= "filename" > file name </param>
<param name= "Content" > Contents </param>
<returns></returns>
public static bool WriteLine (string fileName, string content)
{
using (FileStream fs = new FileStream (FileName, FileMode.OpenOrCreate | Filemode.append))
{
Lock (FS)
{
if (!fs. CanWrite)
{
throw new System.Security.SecurityException ("File filename=" + FileName + "is a read-only file cannot be written!");
}

StreamWriter SW = new StreamWriter (FS);
Sw. WriteLine (content);
Sw. Dispose ();
Sw. Close ();
return true;
}
}
}

public static bool Copydir (DirectoryInfo Fromdir, String todir)
{
Return Copydir (Fromdir, Todir, fromdir.fullname);
}


<summary>
Copy Directory
</summary>
<param name= "Fromdir" > Directories to be copied </param>
<param name= "Todir" > copied to the Directory </param>
<returns></returns>
public static bool Copydir (string fromdir, String todir)
{
if (Fromdir = = NULL | | todir = = NULL)
{
throw new NullReferenceException ("parameter is empty");
}

if (Fromdir = = Todir)
{
throw new Exception ("Two directories are" + fromdir);
}

if (! Directory.Exists (Fromdir))
{
throw new IOException ("Directory fromdir=" +fromdir+ "does not exist");
}

DirectoryInfo dir = new DirectoryInfo (fromdir);
Return Copydir (dir, Todir, dir. FullName);
}


<summary>
Copy Directory
</summary>
<param name= "Fromdir" > Directories to be copied </param>
<param name= "Todir" > copied to the Directory </param>
<param name= "RootDir" > The root directory to be copied </param>
<returns></returns>
private static bool Copydir (DirectoryInfo Fromdir, String todir, String rootdir)
{
String FilePath = String. Empty;
foreach (FileInfo f in Fromdir.getfiles ())
{
FilePath = Todir + f.fullname.substring (rootdir.length);
String newdir = filepath.substring (0, filepath.lastindexof ("\ \"));
Createdir (Newdir);
File.Copy (F.fullname, FilePath, true);
}

foreach (DirectoryInfo dir in Fromdir.getdirectories ())
{
Copydir (dir, Todir, RootDir);
}

return true;
}


<summary>
deleting files
</summary>
<param name= the full path to the filename > file </param>
<returns></returns>
public static bool DeleteFile (string fileName)
{
if (Exists (fileName))
{
File.delete (FileName);
return true;
}
return false;
}


public static void Deletedir (DirectoryInfo dir)
{
if (dir = = null)
{
throw new NullReferenceException ("directory does not exist");
}

foreach (DirectoryInfo d in dir. GetDirectories ())
{
Deletedir (d);
}

foreach (FileInfo f in dir. GetFiles ())
{
DeleteFile (F.fullname);
}

Dir. Delete ();

}


<summary>
Delete Directory
</summary>
<param name= "dir" > Development directory </param>
<param name= "Onlydir" > whether to delete only directories </param>
<returns></returns>
public static bool Deletedir (string dir, bool Onlydir)
{
if (dir = = null | | dir. Trim () = = "")
{
throw new NullReferenceException ("directory dir=" + dir + "does not exist");
}

if (! Directory.Exists (dir))
{
return false;
}

DirectoryInfo dirinfo = new DirectoryInfo (dir);
if (Dirinfo.getfiles (). Length = = 0 && dirinfo.getdirectories (). length==0)
{
Directory.delete (dir);
return true;
}


if (!onlydir)
{
return false;
}
Else
{
Deletedir (Dirinfo);
return true;
}

}


<summary>
Find a file in the specified directory
</summary>
<param name= "dir" > Directory </param>
<param name= "filename" > file name </param>
<returns></returns>
public static bool FindFile (String dir, String fileName)
{
if (dir = = null | | dir. Trim () = = "" | | FileName = = NULL | | Filename.trim () = = "" | |! Directory.Exists (dir))
{
return false;
}

DirectoryInfo dirinfo = new DirectoryInfo (dir);
Return FindFile (Dirinfo, fileName);

}


public static bool FindFile (DirectoryInfo dir, string fileName)
{
foreach (DirectoryInfo d in dir. GetDirectories ())
{
if (file.exists (d.fullname + "\ \" + fileName)
{
return true;
}
FindFile (D,filename);
}

return false;
}

}
}

C # File Operation classes

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.