nx-installation_projects_vi.../NX-Installation/EvaluateFreeDiscSpace.cs

253 lines
8.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NX_Installation
{
class EvaluateFreeDiskSpace
{
private string FileName { set; get; }
//Setzen des Logfile Dateinamens
/// <summary>
/// Sets the logfile filename
/// </summary>
/// <param name="filename">Path and filename. E.g.: "C:\TMP\NX-Installation.log"</param>
public EvaluateFreeDiskSpace(string filename)
{
FileName = filename;
}
const string laufwerk = "C";
//Evaluieren ob genügend Platz auf Laufwerk C:\ frei ist
/// <summary>
/// Evaluate whether enough free disk space is available.
/// </summary>
/// <param name="nxInstallFolder">This folder includes all NX installation files.</param>
/// <param name="nxDocInstallFolder">This folder includes all NX-Documentation installation files.</param>
/// <param name="NxDocumentation">Is <value>True</value> if doumentation will be installed. Is <value>False</value> if documentation will not be installed.</param>
/// <returns>If an error occurs or not enough free disk space is available, it is <value>True</value>, otherwise it is <value>False</value>.</returns>
public bool EvaluateDiskSpace(string nxInstallFolder, string nxDocInstallFolder, bool NxDocumentation)
{
Logfile log = new Logfile(FileName);
//log.Info("Evaluate free disk space on drive C:");
bool error = false;
//Freien Platz auf Laufwerk C:\ ermitteln
long free = DetermineFreeSpace(laufwerk);
if (free == -1)
{
error = true;
log.Error("Was not able to calculated free disk space on drive C:");
}
else
{
log.Info("Free disk space on drive C (calculated): " + free.ToString("#,####") + " Bytes");
}
//Größe des NX Installationsverzeichnisses ermitteln
long nxInstallSize = GetDirectorySize(nxInstallFolder);
if (nxInstallSize == -1)
{
error = true;
log.Error("Was not able to calculated NX installation file size");
}
else
{
log.Info("NX installation file size on disk (calculated): " + nxInstallSize.ToString("#,####") + " Bytes");
}
//Größe des NX Doc Installationsverzeichnisses ermitteln
long nxDocInstallSize;
if (NxDocumentation)
{
nxDocInstallSize = GetDirectorySize(nxDocInstallFolder);
if (nxDocInstallSize == -1)
{
error = true;
log.Error("Was not able to calculated NX documentation installation file size");
}
else
{
log.Info("NX documentation installation file size on disk (calculated): " + nxDocInstallSize.ToString("#,####") + " Bytes");
}
}
else
{
nxDocInstallSize = 0;
}
//NX Installationsfolder und NX Doc Installationsfolder addieren
long sizeInstallFolders = AddSizes(nxInstallSize, nxDocInstallSize);
if (sizeInstallFolders == -1)
{
error = true;
log.Error("Was not able to summarize NX installation file size and NX documentation installation file size");
}
else
{
log.Info("Total installation file size on disk (calculated): " + sizeInstallFolders.ToString("#,####") + " Bytes");
}
//Notwendigen Platzbedarf ermitteln = 4 mal die Summe aller Installationsdateien
long minSize = CalculateMinimumSize(sizeInstallFolders);
if (minSize == -4)
{
error = true; //Weil -1 in der Method mal 4 gerechnet wird
log.Error("Was not able to calculate the needed minimum free disk space");
}
else
{
log.Info("Minimum free disk space on drive C needed (calculated): " + minSize.ToString("#,####") + " Bytes");
}
//Differenz zwischen freiem und notwendigen Platz ermitteln und prüfen ob genug Platz frei ist
long diff = CalculateDifference(free, minSize);
if (diff < 0)
{
error = true; //Ist diff < 0, dann ist ein Fehler aufgetreten oder die Differenz ein Minusbtrag (zu wenig Platz)
if (diff == -1)
{
log.Error("Was not able to calculate the difference between free disk space and necessary disk space");
}
else
{
MessageBox.Show("Not enough disk space free!\nYou need at least\n" + minSize.ToString("#,####") + " Bytes\ndisk space free.\n\nPlease contact your\nlocal administrator.",
"Error!",
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
log.Error("Not enough disk space free on drive C. At least " + sizeInstallFolders.ToString("#,####") + " Bytes additional free disk space on drice C will be needed.");
}
}
else
{
log.Info("Enough disk space free on drive C");
}
return error;
}
public long GetFreeSpace(string deviceName)
{
long frei = 0;
try
{
//Freien Speicherplatz ausgeben
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady)
{
if (d.Name == (deviceName + ":\\"))
{
frei = d.TotalFreeSpace;
}
}
}
}
catch
{
frei = -1;
}
return frei;
}
private long DetermineFreeSpace(string laufwerk)
{
// Alle Informationen eines bestimmten Laufwerks anzeigen
long value;
try
{
value = GetFreeSpace(laufwerk);
if (!(value >= 0)) value = -1;
}
catch
{
value = -1;
}
return value;
}
private long GetDirectorySize(string path)
{
long size = 0;
// get sub directories (recursive)
try
{
string[] subDirectories = Directory.GetDirectories(path);
foreach (string subDirectory in subDirectories)
size += GetDirectorySize(subDirectory);
}
catch
{
size = -1;
}
// get files and add size
try
{
string[] fileNames = Directory.GetFiles(path);
foreach (string fileName in fileNames)
{
FileInfo fileInfo = new FileInfo(fileName);
size += fileInfo.Length;
}
}
catch
{
size = -1;
}
return size;
}
private long AddSizes(long nxInstallSize, long nxDocInstallSize)
{
if (nxInstallSize != -1 & nxDocInstallSize != -1)
{
return nxInstallSize + nxDocInstallSize;
}
else
{
return -1;
}
}
private long CalculateMinimumSize(long sizeInstallFolders)
{
return sizeInstallFolders * 4;
}
private long CalculateDifference(long free, long sizeInstallFolders)
{
if (free != -1 & sizeInstallFolders != -1)
{
if ((free - sizeInstallFolders) == -1)
{
return -2;
}
else
{
return free - sizeInstallFolders;
}
}
else
{
return -1;
}
}
}
}