[英]How to get file details using C# on a remote machine with username and password
Tried to search for something similar but could not find.试图搜索类似的东西,但找不到。
I have file on a remote machine which I want to query for it`s details like version, date, etc. How do I do that when I need to enter credentials for that machine?我在远程机器上有文件,我想查询它的详细信息,如版本、日期等。当我需要输入该机器的凭据时,我该怎么做? FileVersionInfo does not give option to do so.
FileVersionInfo 没有提供这样做的选项。
Thanks谢谢
Update:更新:
As I said above I checked what FIleVersionInfo gives me (and also tried it) and that will not work for me.正如我上面所说,我检查了 FIleVersionInfo 给我的内容(并尝试了它),但这对我不起作用。 I also tried using WMI and failed with (although it looked like the direction I need) Here is the WMI code I tried - haven`t got much far:
我也尝试过使用 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
}
I mainly need the file properties version and date.我主要需要文件属性版本和日期。
Thanks谢谢
Thanks to @Draken comment above I`ve added missing properties on the ConnectionOptions and also fixed my mistake on the domain name.感谢上面的@Draken 评论,我在 ConnectionOptions 上添加了缺失的属性,并修复了我在域名上的错误。
Here is the code I use to get to the file in the network PC这是我用来访问网络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.