ec/Adaptation/Shared/Drive.cs

79 lines
2.7 KiB
C#

using log4net;
using System;
using System.Diagnostics;
using System.Text;
namespace Adaptation.Shared
{
public class Drive : Methods.IDrive
{
public bool Use { get; set; }
public string Letter { get; set; }
public string Share { get; set; }
public string User { get; set; }
public string Password { get; set; }
void Methods.IDrive.MapEafDrives(ILog log, Properties.IAppSettings appSettings)
{
MapEafDrives(log, appSettings);
}
public static void MapEafDrives(ILog log, Properties.IAppSettings appSettings)
{
Process process;
string decrypted;
string arguments;
string[] segments;
string standardError;
string standardOutput;
string fileName = "net";
StringBuilder stringBuilder = new();
foreach (Drive drive in appSettings.Drives)
{
if (!drive.Use)
continue;
if (string.IsNullOrEmpty(drive.Password))
decrypted = string.Empty;
else
decrypted = RijndaelEncryption.Decrypt(drive.Password, appSettings.Company);
arguments = $"use {drive.Letter}: \"{drive.Share}\" /p:yes /user:{drive.User} {decrypted}";
stringBuilder.Clear();
segments = arguments.Split(' ');
for (int j = 0; j < segments.Length - 1; j++)
stringBuilder.Append(segments[j]).Append(' ');
log.Info($"// {stringBuilder}");
ProcessStartInfo processStartInfo = new(fileName, arguments)
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false
};
try
{
process = Process.Start(processStartInfo);
for (int j = 1; j < 45; j++)
{
process.WaitForExit(1000);
if (process.HasExited)
break;
}
if (!process.HasExited)
log.Error("// Never exited!");
else
{
standardError = process.StandardError.ReadToEnd();
standardOutput = process.StandardOutput.ReadToEnd();
log.Info($"// {standardError}{Environment.NewLine}{Environment.NewLine}{"// "}{standardOutput}");
}
}
catch (Exception ex)
{
log.Error("Error:", ex);
}
}
}
}
}