Background: I'm totally new to c#, and coding in general (but I've been around and decided to teach myself what I need for this effort).
For my fleet of 14 critical servers, each server has a backup folder for one other server at another site. Disk space consumption is something that can bring a server down and have really bad consequences for a lot of people.
I am putting in a monthly trend process to improve detection of not only disk usage issues but to identify specific folders growing unexpectedly over any given month. I've narrowed the list of folders down to certain OS and application folders to record size data for my trend analysis and I am in the initial stages of making a small executable that will record just the data I need and write it to a text file that my VBA spreadsheet will pull in once a month.
I've gotten the application working with an embedded static folder list, but each server has one different folder name from the next, meaning I will need to make a server-specific executable due to part of a folder name being related to the specific other server it has daily copied backup files from. I would rather have just one app that is able to be used on any of these servers and figure out what it's partner servers backup folder name is by a wildcard searching part of the folder name string.
My current version of code that creates a text file with the needed information is below. The specific string I would like to replace to use a wildcard search to define the substituted value is: DirectoryInfo dInfo5 = new DirectoryInfo(@"C:/_Backup_server02");
If it was easy something like: DirectoryInfo dInfo5 = new DirectoryInfo(@"C:/_Backup_server*");
would work, but that's not working so I'm willing to go a more complicated route.
Any ideas would be appreciated. Code:
using System;
using System.Linq;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string server = System.Environment.MachineName;
DriveInfo CDrive = new DriveInfo("C");
if (CDrive.IsReady)
{
double freeSpacePerc = (CDrive.TotalFreeSpace / (float)CDrive.TotalSize) * 100;
StreamWriter sw = new StreamWriter("C:\\"+server+" Diskdata.txt");
DirectoryInfo dInfo = new DirectoryInfo(@"C:/inetpub/logs/LogFiles");
long sizeOfDir = DirectorySize(dInfo, true);
DirectoryInfo dInfo1 = new DirectoryInfo(@"C:\SQLServerData");
long sizeOfDir1 = DirectorySize(dInfo1, true);
DirectoryInfo dInfo2 = new DirectoryInfo(@"C:/temp");
long sizeOfDir2 = DirectorySize(dInfo2, true);
DirectoryInfo dInfo3 = new DirectoryInfo(@"C:/NTDahsHome");
long sizeOfDir3 = DirectorySize(dInfo3, true);
DirectoryInfo dInfo4 = new DirectoryInfo(@"C:/windows/winsxs");
long sizeOfDir4 = DirectorySize(dInfo4, true);
DirectoryInfo dInfo5 = new DirectoryInfo(@"C:/_Backup_server02");
long sizeOfDir5 = DirectorySize(dInfo5, true);
DirectoryInfo dInfo6 = new DirectoryInfo(@"C:/Backup");
long sizeOfDir6 = DirectorySize(dInfo6, true);
DirectoryInfo dInfo7 = new DirectoryInfo(@"C:/mindata");
long sizeOfDir7 = DirectorySize(dInfo7, true);
try
{
sw.WriteLine(server);
sw.WriteLine("DrivePercentFree: {0:0.00}", freeSpacePerc);
sw.WriteLine ("TotalFreeSpace: {0}", CDrive.TotalFreeSpace);
sw.WriteLine ("inetpublogfiles: " + "{0:N0}", sizeOfDir);
sw.WriteLine ("SQLServerData : " + sizeOfDir1);
sw.WriteLine ("temp: " + sizeOfDir2);
sw.WriteLine ("NTDahsHome: " + sizeOfDir3);
sw.WriteLine ("winsxs: " + sizeOfDir4);
sw.WriteLine ("_backup_jeacemssj02: " + sizeOfDir5);
sw.WriteLine ("backup: " + sizeOfDir6);
sw.WriteLine ("mindata: " + sizeOfDir7);
sw.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
static long DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
{
long totalSize = dInfo.EnumerateFiles()
.Sum(file => file.Length);
if (includeSubDir)
{
totalSize += dInfo.EnumerateDirectories()
.Sum(dir => DirectorySize(dir, true));
}
return totalSize;
}
static long DirectorySize1(DirectoryInfo dInfo1, bool includeSubDir)
{
long totalSize1 = dInfo1.EnumerateFiles()
.Sum(file => file.Length);
if (includeSubDir)
{
totalSize1 += dInfo1.EnumerateDirectories()
.Sum(dir => DirectorySize(dir, true));
}
return totalSize1;
}
static long DirectorySize2(DirectoryInfo dInfo2, bool includeSubDir)
{
long totalSize2 = dInfo2.EnumerateFiles()
.Sum(file => file.Length);
if (includeSubDir)
{
totalSize2 += dInfo2.EnumerateDirectories()
.Sum(dir => DirectorySize(dir, true));
}
return totalSize2;
}
static long DirectorySize3(DirectoryInfo dInfo3, bool includeSubDir)
{
long totalSize3 = dInfo3.EnumerateFiles()
.Sum(file => file.Length);
if (includeSubDir)
{
totalSize3 += dInfo3.EnumerateDirectories()
.Sum(dir => DirectorySize(dir, true));
}
return totalSize3;
}
static long DirectorySize4(DirectoryInfo dInfo4, bool includeSubDir)
{
long totalSize4 = dInfo4.EnumerateFiles()
.Sum(file => file.Length);
if (includeSubDir)
{
totalSize4 += dInfo4.EnumerateDirectories()
.Sum(dir => DirectorySize(dir, true));
}
return totalSize4;
}
static long DirectorySize5(DirectoryInfo dInfo5, bool includeSubDir)
{
long totalSize5 = dInfo5.EnumerateFiles()
.Sum(file => file.Length);
if (includeSubDir)
{
totalSize5 += dInfo5.EnumerateDirectories()
.Sum(dir => DirectorySize(dir, true));
}
return totalSize5;
}
static long DirectorySize6(DirectoryInfo dInfo6, bool includeSubDir)
{
long totalSize6 = dInfo6.EnumerateFiles()
.Sum(file => file.Length);
if (includeSubDir)
{
totalSize6 += dInfo6.EnumerateDirectories()
.Sum(dir => DirectorySize(dir, true));
}
return totalSize6;
}
static long DirectorySize7(DirectoryInfo dInfo7, bool includeSubDir)
{
long totalSize7 = dInfo7.EnumerateFiles()
.Sum(file => file.Length);
if (includeSubDir)
{
totalSize7 += dInfo7.EnumerateDirectories()
.Sum(dir => DirectorySize(dir, true));
}
return totalSize7;
}
}
}