using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.Core.Helpers
{
///
/// Contains several path helper methods.
///
public static class PathHelper
{
///
/// Gets the user tango folder (%appdata%\Twine\Tango).
///
///
public static String GetUserTangoFolder()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango");
}
///
/// Gets the application startup path.
///
///
public static String GetStartupPath()
{
return AppDomain.CurrentDomain.BaseDirectory;
}
///
/// Gets the solution folder.
///
///
public static String GetSolutionFolder()
{
DirectoryInfo path = new DirectoryInfo(GetStartupPath());
while (path.Name != "Visual_Studio")
{
path = path.Parent;
}
return path.FullName;
}
///
/// Tries to delete the specified file.
///
/// The file path.
///
public static bool TryDeleteFile(String path)
{
try
{
File.Delete(path);
return true;
}
catch
{
return false;
}
}
///
/// Copies the specified directory source path to the specified destination.
///
/// The source path.
/// The destination path.
/// if set to true will copy sub directories.
/// Source directory does not exist or could not be found: "
/// + sourcePath
public static void CopyDirectory(string sourcePath, string destinationPath, bool copySubDirs, Action progress = null)
{
CopyDirectoryInternal(sourcePath, destinationPath, copySubDirs, progress, Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories).Length, 0);
}
private static void CopyDirectoryInternal(string sourcePath, string destinationPath, bool copySubDirs, Action progress, int total, int current)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourcePath);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourcePath);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destinationPath))
{
Directory.CreateDirectory(destinationPath);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destinationPath, file.Name);
file.CopyTo(temppath, false);
current++;
progress?.Invoke(current, total);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destinationPath, subdir.Name);
CopyDirectoryInternal(subdir.FullName, temppath, copySubDirs, progress, total, current);
}
}
}
}
}