Last active
September 18, 2017 15:30
-
-
Save DJJonnyFeeke/6e211c8ec8fa1b38da49e2fb46925081 to your computer and use it in GitHub Desktop.
File and Folder - Create/Copy/Move/Delete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///I tried to create a compact way to perform all the functions and return proper messages when necessary. | |
/// | |
using System; | |
using System.IO; | |
using System.Runtime.Serialization; | |
/// <summary> | |
/// File Methods | |
/// </summary> | |
public class TunerFileMethods | |
{ | |
#region Fields | |
/// <summary> | |
/// The file success | |
/// </summary> | |
private const string FileSuccess = @"File Created"; | |
/// <summary> | |
/// The file exists | |
/// </summary> | |
private const string FileExists = @"File Exists"; | |
/// <summary> | |
/// The file name not found | |
/// </summary> | |
private const string FileNameNotFound = @"FileName not Found"; | |
/// <summary> | |
/// The file not deleted | |
/// </summary> | |
private const string FileNotDeleted = @"File Not Deleted: Error Message:"; | |
/// <summary> | |
/// The folder success | |
/// </summary> | |
private const string FolderSuccess = @"Folder Created"; | |
/// <summary> | |
/// The folder exists | |
/// </summary> | |
private const string FolderExists = @"Folder Exists"; | |
/// <summary> | |
/// The folder name not found | |
/// </summary> | |
private const string FolderNameNotFound = @"FileName not Found"; | |
/// <summary> | |
/// The folder not deleted | |
/// </summary> | |
private const string FolderNotDeleted = @"File Not Deleted: Error Message:"; | |
/// <summary> | |
/// The unknown type | |
/// </summary> | |
private const string UnknownType = @"Unknown Type"; | |
/// <summary> | |
/// The unknown function | |
/// </summary> | |
private const string UnknownFunction = @"Unknown Function"; | |
/// <summary> | |
/// The source file name | |
/// </summary> | |
public string SourceFileName; | |
/// <summary> | |
/// The source path | |
/// </summary> | |
public string SourcePath; | |
/// <summary> | |
/// The target file name | |
/// </summary> | |
public string TargetFileName; | |
/// <summary> | |
/// The target path | |
/// </summary> | |
public string TargetPath; | |
/// <summary> | |
/// Method Functions | |
/// </summary> | |
public enum MethodFunctions | |
{ | |
Create, Copy, Move, Delete | |
} | |
/// <summary> | |
/// Method Types | |
/// </summary> | |
public enum MethodTypes | |
{ | |
File, Folder | |
} | |
#endregion Fields | |
#region File Functions | |
/// <summary> | |
/// Creates the specified type. | |
/// </summary> | |
/// <param name="type">The type.</param> | |
/// <param name="location">The location.</param> | |
/// <param name="function">The function.</param> | |
/// <returns></returns> | |
public Tuple<bool,string> FileSystem(MethodTypes type, Tuple<string, string> location, MethodFunctions function) | |
{ | |
try | |
{ | |
switch (type) | |
{ | |
case MethodTypes.File: | |
switch (function) | |
{ | |
case MethodFunctions.Create: | |
if (!File.Exists(location.Item1)) | |
{ | |
File.Create(location.Item1); | |
return Tuple.Create(true, FileSuccess); | |
} | |
else | |
{ | |
return Tuple.Create(false, FileExists); | |
} | |
case MethodFunctions.Copy: | |
if (!File.Exists(location.Item2)) | |
{ | |
SourceFileName = Path.GetFileName(location.Item1); | |
if (SourceFileName == null) return Tuple.Create(false, FileNameNotFound); | |
TargetFileName = Path.Combine(location.Item2, SourceFileName); | |
File.Copy(location.Item1, TargetFileName); | |
return Tuple.Create(true, FileSuccess); | |
} | |
else | |
{ | |
return Tuple.Create(false, FileExists); | |
} | |
case MethodFunctions.Move: | |
if (!File.Exists(location.Item2)) | |
{ | |
File.Move(location.Item1, location.Item2); | |
return Tuple.Create(true, FileSuccess); | |
} | |
else | |
{ | |
return Tuple.Create(false, FileExists); | |
} | |
case MethodFunctions.Delete: | |
if (File.Exists(location.Item1)) | |
{ | |
try | |
{ | |
File.Delete(location.Item1); | |
return Tuple.Create(true, FileSuccess); | |
} | |
catch (IOException e) | |
{ | |
return Tuple.Create(false, FileNotDeleted + e.Message); | |
} | |
} | |
else | |
{ | |
return Tuple.Create(false, FileNameNotFound); | |
} | |
default: | |
return Tuple.Create(false, UnknownFunction); | |
} | |
case MethodTypes.Folder: | |
switch (function) | |
{ | |
case MethodFunctions.Create: | |
if (!Directory.Exists(location.Item1)) | |
{ | |
Directory.CreateDirectory(location.Item1); | |
return Tuple.Create(true, FolderSuccess); | |
} | |
else | |
{ | |
return Tuple.Create(false, FolderExists); | |
} | |
case MethodFunctions.Copy: | |
if (!Directory.Exists(location.Item2)) | |
{ | |
DirectoryInfo directorySource = new DirectoryInfo(location.Item1); | |
DirectoryInfo directoryTarget = new DirectoryInfo(location.Item2); | |
RecursiveDirectoryCopy(directoryTarget, directorySource); | |
return Tuple.Create(true, FolderSuccess); | |
} | |
else | |
{ | |
return Tuple.Create(false, FolderExists); | |
} | |
case MethodFunctions.Move: | |
if (!Directory.Exists(location.Item2)) | |
{ | |
Directory.Move(location.Item1, location.Item2); | |
return Tuple.Create(true, FolderSuccess); | |
} | |
else | |
{ | |
return Tuple.Create(false, FolderExists); | |
} | |
case MethodFunctions.Delete: | |
if (Directory.Exists(location.Item1)) | |
{ | |
try | |
{ | |
Directory.Delete(location.Item1); | |
return Tuple.Create(true, FolderSuccess); | |
} | |
catch (IOException e) | |
{ | |
return Tuple.Create(false, FolderNotDeleted + e.Message); | |
} | |
} | |
else | |
{ | |
return Tuple.Create(false, FolderNameNotFound); | |
} | |
default: | |
return Tuple.Create(false, UnknownFunction); | |
} | |
default: | |
return Tuple.Create(false, UnknownType); | |
} | |
} | |
catch (Exception e) | |
{ | |
return Tuple.Create(false, e.Message); | |
} | |
} | |
/// <summary> | |
/// Recursives the directory copy. | |
/// </summary> | |
/// <param name="directoryTarget">The directory target.</param> | |
/// <param name="directorySource">The directory source.</param> | |
private static void RecursiveDirectoryCopy(DirectoryInfo directoryTarget, DirectoryInfo directorySource) | |
{ | |
Directory.CreateDirectory(directoryTarget.FullName); | |
foreach (FileInfo file in directorySource.GetFiles()) | |
{ | |
file.CopyTo(Path.Combine(directoryTarget.FullName, file.Name), true); | |
} | |
foreach (DirectoryInfo directorySourceSubDir in directorySource.GetDirectories()) | |
{ | |
DirectoryInfo nextTargetSubDir = directoryTarget.CreateSubdirectory(directorySourceSubDir.Name); | |
RecursiveDirectoryCopy(directorySourceSubDir, nextTargetSubDir); | |
} | |
} | |
#endregion File Functions | |
#region Database Functions | |
#endregion Database Functions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment