[Resolvido] Pegar Espaço em Disco em C#
Bom dia,
estou desenvolvendo um Aplicativo Windows que pega o espaço em disco de diversos servidores da rede, sendo que eu tenho 4 servidores que estão em outro domínio, ex: 10 servidores no domínio AA e 4 servidores no domínio BB e o aplicativo roda no domínio AA. quando eu tento pegar os dados do domínio BB da Acesso Negado. Estou postando o código abaixo, se alguém puder me ajudar agradeceria muito.
ConnectionOptions opt = new ConnectionOptions();
ObjectQuery oQuery = new ObjectQuery("SELECT Size, FreeSpace, Name, FileSystem FROM Win32_LogicalDisk WHERE DriveType = 3");
ManagementScope scope = new ManagementScope("\\\\<meu servidor>\\root\\cimv2", opt);
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(scope, oQuery);
ManagementObjectCollection collection = moSearcher.Get();
foreach (ManagementObject res in collection)
{
if (res["Name"].ToString() == "C:")
{
decimal size = Convert.ToDecimal(res["Size"]) / 1024 / 1024 / 1024;
decimal freeSpace = Convert.ToDecimal(res["FreeSpace"]) / 1024 / 1024 / 1024;
string unidade = res["Name"].ToString();
decimal tamanho = Decimal.Round(size, 2);
decimal livre = Decimal.Round(freeSpace, 2);
decimal usado = Decimal.Round(size - freeSpace, 2);
decimal livrepercent = Decimal.Round(usado / size, 2) * 100;
if (livrepercent > parametro)
{
unTPC.ForeColor = Color.Red;
tamTPC.ForeColor = Color.Red;
liTPC.ForeColor = Color.Red;
imgTPC.Visible = true;
SystemSounds.Question.Play();
}
unTPC.Text = unidade;
unTPC.Refresh();
unTPC.Visible = true;
tamTPC.Text = tamanho.ToString();
tamTPC.Refresh();
tamTPC.Visible = true;
liTPC.Text = livre.ToString();
liTPC.Refresh();
liTPC.Visible = true;
}
if (res["Name"].ToString() == "D:")
{
decimal size = Convert.ToDecimal(res["Size"]) / 1024 / 1024 / 1024;
decimal freeSpace = Convert.ToDecimal(res["FreeSpace"]) / 1024 / 1024 / 1024;
string unidade = res["Name"].ToString();
decimal tamanho = Decimal.Round(size, 2);
decimal livre = Decimal.Round(freeSpace, 2);
decimal usado = Decimal.Round(size - freeSpace, 2);
decimal livrepercent = Decimal.Round(usado / size, 2) * 100;
if (livrepercent > parametro)
{
un2TPC.ForeColor = Color.Red;
tam2TPC.ForeColor = Color.Red;
li2TPC.ForeColor = Color.Red;
imgTPC.Visible = true;
SystemSounds.Question.Play();
}
un2TPC.Text = unidade;
un2TPC.Refresh();
un2TPC.Visible = true;
tam2TPC.Text = tamanho.ToString();
tam2TPC.Refresh();
tam2TPC.Visible = true;
li2TPC.Text = livre.ToString();
li2TPC.Refresh();
li2TPC.Visible = true;
}
}Discussão (2)
Carregando comentários...