Files
PowerToys/src/modules/launcher/Wox.Plugin/SharedCommands/FilesFolders.cs
Avneet Kaur ca1e5d111a [fxcop] Fixes for Wox.Plugin (1of3) (#7457)
* Added CultureInfo (CA1307: Specify StringComparison for clarity / CA1304: Specify CultureInfo)

* Check arguments and throw ArgumentNullException (CA1062: Validate arguments of public methods)

* Changed url parameter from string to System.Uri and added null checks (CA1054: URI parameters should not be strings)

* Rethrow exception without specifying the exception explicitly (CA2200: Rethrow to preserve stack details)

* Changed from Collection property to methods for PluginMetadata::ActionKeywords (CA2227: Collection properties should be read only)

* Changed from Collection property to methods for Result::GetTitleHighlightData (CA2227: Collection properties should be read only)

* Made Collection property read-only and added parameter in constructor for Result::SubTitleHighlightData (CA2227: Collection properties should be read only)

* Made Collection property read only and added parameter in constructor for ResultUpdatedEventArgs::Results (CA2227: Collection properties should be read only)

* CA1507: Use nameof in place of string

* Removed initialization for ThemeManager::_disposed (CA1805: Do not initialize unnecessarily)

* Changed Query::Terms array property to ReadOnlyCollection and added private set (CA1819: Properties should not return arrays)

* CA1060: Move P/Invokes to NativeMethods class

* CA1806: Do not ignore method results

* CA2101: Specify marshaling for P/Invoke string arguments

* Removed unnecessary empty interface IFeatures (CA1040: Avoid empty interfaces)

- Removed IFeatures interface and references
- Renamed IFeatures.cs to IContextMenu.cs according to guidelines

* Added comments for CultureInfo (CA1307: Specify StringComparison for clarity / CA1304: Specify CultureInfo)

* Added localization for Wox.Plugin and localized strings in FilesFolders.cs
2020-10-26 15:14:33 -07:00

132 lines
5.3 KiB
C#

// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using Wox.Plugin.Logger;
using Wox.Plugin.Properties;
namespace Wox.Plugin.SharedCommands
{
public static class FilesFolders
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Exception has been logged")]
public static void Copy(this string sourcePath, string targetPath)
{
// 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);
}
try
{
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
// 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(targetPath, file.Name);
file.CopyTo(temppath, false);
}
// Recursively copy subdirectories by calling itself on each subdirectory until there are no more to copy
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(targetPath, subdir.Name);
Copy(subdir.FullName, temppath);
}
}
#pragma warning disable CS0168 // Variable is declared but never used. Due to #if debug vs release statement
catch (Exception e)
#pragma warning restore CS0168 // Variable is declared but never used
{
string error = $"Copying path {targetPath} has failed";
Log.Exception(error, e, MethodBase.GetCurrentMethod().DeclaringType);
#if DEBUG
throw;
#else
// Using CurrentCulture since this is user facing
System.Windows.MessageBox.Show(string.Format(CultureInfo.CurrentCulture, Resources.filesfolder_copy_failed, targetPath));
RemoveFolder(targetPath);
#endif
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Exception has been logged")]
public static bool VerifyBothFolderFilesEqual(this string fromPath, string toPath)
{
try
{
var fromDir = new DirectoryInfo(fromPath);
var toDir = new DirectoryInfo(toPath);
if (fromDir.GetFiles("*", SearchOption.AllDirectories).Length != toDir.GetFiles("*", SearchOption.AllDirectories).Length)
{
return false;
}
if (fromDir.GetDirectories("*", SearchOption.AllDirectories).Length != toDir.GetDirectories("*", SearchOption.AllDirectories).Length)
{
return false;
}
return true;
}
#pragma warning disable CS0168 // Variable is declared but never used. Due to #if debug vs release statement
catch (Exception e)
#pragma warning restore CS0168 // Variable is declared but never used
{
string error = $"Unable to verify folders and files between {fromPath} and {toPath}";
Log.Exception(error, e, MethodBase.GetCurrentMethod().DeclaringType);
#if DEBUG
throw;
#else
// Using CurrentCulture since this is user facing
System.Windows.MessageBox.Show(string.Format(CultureInfo.CurrentCulture, Resources.filesfolder_verifybothfolderfilesequal_failed, fromPath, toPath));
return false;
#endif
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Exception has been logged")]
public static void RemoveFolder(this string path)
{
try
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
}
#pragma warning disable CS0168 // Variable is declared but never used. Due to #if debug vs release statement
catch (Exception e)
#pragma warning restore CS0168 // Variable is declared but never used
{
string error = $"Not able to delete folder {path}";
Log.Exception(error, e, MethodBase.GetCurrentMethod().DeclaringType);
#if DEBUG
throw;
#else
// Using CurrentCulture since this is user facing
System.Windows.MessageBox.Show(string.Format(CultureInfo.CurrentCulture, Resources.filesfolder_removefolder_failed, path));
#endif
}
}
}
}