[英]How to get file details using C# on a remote machine with username and password
试图搜索类似的东西,但找不到。
我在远程机器上有文件,我想查询它的详细信息,如版本、日期等。当我需要输入该机器的凭据时,我该怎么做? FileVersionInfo 没有提供这样做的选项。
谢谢
更新:
正如我上面所说,我检查了 FIleVersionInfo 给我的内容(并尝试了它),但这对我不起作用。 我也尝试过使用 WMI 并且失败了(虽然它看起来像我需要的方向)这是我尝试过的 WMI 代码 - 还没有走多远:
var computerName = "IP_ADDRESS";
ConnectionOptions conn = new ConnectionOptions();
conn.Username = "username";
conn.Password = "password";
conn.Authority = "ntlmdomain:DOMAIN";
conn.Impersonation = ImpersonationLevel.Impersonate;
conn.EnablePriviledges = true;
var scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", computerName), conn);
scope.Connect();
string Drive = "c:";
string Path = "\\\\inetpub\\\\wwwroot\\\\FOLDER\\\BIN\\\File.dll";
ObjectQuery Query = new ObjectQuery(string.Format("SELECT * FROM CIM_DataFile Where Drive='{0}' AND Path='{1}' ", Drive, Path));
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(scope, Query);
foreach (ManagementObject WmiObject in Searcher.Get())
{
Console.WriteLine("{0}", (string)WmiObject["Name"]);// String
}
我主要需要文件属性版本和日期。
谢谢
感谢上面的@Draken 评论,我在 ConnectionOptions 上添加了缺失的属性,并修复了我在域名上的错误。
这是我用来访问网络PC中的文件的代码
var computerName = "IP_ADDRESS";
ConnectionOptions conn = new ConnectionOptions();
conn.Username = "username";
conn.Password = "password";
conn.Authority = "ntlmdomain:DOMAIN";
conn.Impersonation = ImpersonationLevel.Impersonate;
conn.EnablePriviledges = true;
var scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", computerName), conn);
scope.Connect();
string Drive = "c:";
string Path = "\\\\inetpub\\\\wwwroot\\\\FOLDER\\\BIN\\\File.dll";
ObjectQuery Query = new ObjectQuery(string.Format("SELECT * FROM CIM_DataFile Where Drive='{0}' AND Path='{1}' ", Drive, Path));
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(scope, Query);
foreach (ManagementObject WmiObject in Searcher.Get())
{
Console.WriteLine("{0}", (string)WmiObject["Name"]);// String
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.