C # File Copy, move, new

Source: Internet
Author: User
Tags file copy flush

The following code can refer to a new mobile delete copy operation for a file in C #, and so on.

<summary>
To determine whether a file or folder exists
</summary>
<param name= "File" > specify file and its path </param>
<param name= "method" > Judgment Mode </param>
<returns> returns whether a Boolean folder exists with True, false does not exist </returns>
public static bool Isexist (string file, Fsomethod method)
{
if (method = = Fsomethod.file)
{
Return file.exists (File);
}
else if (method = = Fsomethod.folder)
{
return directory.exists (file);
}
Else
{
return false;
}
}

#region "New"

<summary>
Create a new file or folder
</summary>
<param name= "File" > file or folder and its path </param>
<param name= "method" > New Way </param>
public static void Create (string file, Fsomethod method)
{
Try
{
if (method = = Fsomethod.file)
{
WriteFile (file, String. Empty);
}
else if (method = = Fsomethod.folder)
{
Directory.CreateDirectory (file);
}
}
Catch
{
throw new UnauthorizedAccessException ("No Permissions!") ");
}
}

#endregion

#region "Copy"

#region "Copy Files"
<summary>
Copy the file and overwrite it if the destination file already exists
</summary>
<param name= "Oldfile" > Source Files </param>
<param name= "NewFile" > Target file </param>
public static void CopyFile (String oldfile, String newFile)
{
File.Copy (Oldfile, NewFile, true);
}

<summary>
Copying a copy file as a stream
</summary>
<param name= "OldPath" > Source Files </param>
<param name= "NewPath" > Target file </param>
<returns> whether replication succeeded true successfully, false failed </returns>
public static bool Copyfilestream (string OldPath, String NewPath)
{
Try
{
Create two FileStream objects
FileStream filestreamold = new FileStream (OldPath, FileMode.Open, FileAccess.Read);
FileStream filestreamnew = new FileStream (NewPath, FileMode.Create, FileAccess.Write);

Create a read-write class separately
BinaryReader br = new BinaryReader (filestreamold);
BinaryWriter bw = new BinaryWriter (filestreamnew);

The pointer to the stream will be read from the file stream
Br. Basestream.seek (0, Seekorigin.begin);
Point to the end of the stream the pointer to the file stream is written
Br. Basestream.seek (0, Seekorigin.end);

while (Br. Basestream.position < Br. Basestream.length)
{
Reads a byte from the BR stream and writes it to the BW stream immediately
Bw. Write (Br. ReadByte ());
}
Release all resources that are occupied
Br. Close ();
Bw. Close ();
Filestreamold.flush ();
Filestreamold.close ();
Filestreamnew.flush ();
Filestreamnew.close ();
return true;
}
Catch
{
return false;
}
}

#endregion

#region "Copy Folder"

<summary>
Copy all content in a folder and all files in its subdirectories
</summary>
<param name= "Olddir" > source folder and its path </param>
<param name= "Newdir" > destination folder and its path </param>
public static void CopyDirectory (String olddir, String newdir)
{
DirectoryInfo DirectoryInfo = new DirectoryInfo (olddir);
Copydirinfo (DirectoryInfo, Olddir, newdir);
}

#endregion

#endregion

#region "Move"

<summary>
Move a file or folder
</summary>
<param name= "Oldfile" > Original file or folder </param>
<param name= "newFile" > Destination files or Folders </param>
<param name= "Method" > Move: 1, for mobile files, 2, for mobile folders </param>
public static void Move (string oldfile, String NewFile, Fsomethod method)
{
if (method = = Fsomethod.file)
{
File.move (Oldfile, newFile);
}

if (method = = Fsomethod.folder)
{
Directory.move (Oldfile, newFile);
}
}

#endregion

#region "Delete"

<summary>
Delete a file or folder
</summary>
<param name= "File" > file or folder and its path </param>
<param name= "method" > Delete mode: 1, for delete files, 2, for delete folder </param>
public static void Delete (string file, Fsomethod method)
{
if (method = = Fsomethod.file)
{
if (file.exists (File))
{
File.delete (file);
}
}

if (method = = Fsomethod.folder)
{
if (directory.exists (file))
{
Directory.delete (file, true); Delete all files and subdirectories in this directory
}
}
}

#endregion

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.