背景:我对c#还是一个陌生的人,并且一般来说都对代码进行编码(但是我一直在这里,并决定教自己该做些什么)。
对于我的由14台关键服务器组成的服务器群,每台服务器都有一个备份文件夹,用于另一站点上的其他服务器。占用磁盘空间会导致服务器瘫痪,并对很多人造成严重后果。
我将每月进行一次趋势处理,以改善对磁盘使用问题的检测,并确定在任何给定月份中意外增长的特定文件夹。我已经将文件夹列表缩小到某些OS和应用程序文件夹以记录趋势分析的大小数据,并且我正处于制作一个小型可执行文件的初期,该可执行文件将仅记录我需要的数据并将其写入文本文件我的VBA电子表格将每月提取一次。
我已经使该应用程序处理嵌入式静态文件夹列表,但是每个服务器都有一个与下一个不同的文件夹名称,这意味着我将需要制作一个服务器特定的可执行文件,因为一部分文件夹名称与每天从中复制备份文件的特定其他服务器。我宁愿只有一个可以在这些服务器中的任何一个上使用的应用程序,并通过通配符搜索部分文件夹名称字符串来弄清楚它的伙伴服务器备份文件夹名称是什么。
下面是我当前使用代码创建带有所需信息的文本文件的代码版本。我想使用通配符搜索来替换以定义替换值的特定字符串是:DirectoryInfo dInfo5 = new DirectoryInfo(@"C:/_Backup_server02");
如果很容易,则类似DirectoryInfo dInfo5 = new DirectoryInfo(@"C:/_Backup_server*");
这样的方法可以工作,但是那不起作用,所以我愿意走更复杂的路线。
任何想法都将不胜感激。代码:
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;
}
}
}
答案 0 :(得分:0)
您可以使用String.Left()
函数(在MSDN上进行查找)仅比较前n个字符,例如:
String folder = [...]; // get backup folder name
if(folder.Left(17) == "C:/_Backup_server")
{
... // do stuff
}
此外,您不需要为每个目录使用单独的DirectorySize
函数。一个适合所有人,只需更改输入参数:
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;
}
在您的主例程中,您只能这样调用:
[...] //previous stuff
[...]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 s[...] // and so on
}