我有一个需要使用WMI运行并访问网络共享的C#exe.但是,当我访问共享时,我得到一个UnauthorizedAccessException.如果我直接运行exe,则可以访问共享.我在两种情况下都使用相同的用户帐户.
我的应用程序有两个部分,一个在本地PC上运行的GUI客户端和一个在远程PC上运行的后端进程.当客户端需要连接到后端时,它首先使用WMI启动远程进程(下面转载的代码).远程进程执行许多操作,包括使用Directory.GetDirectories()访问网络共享并向客户端报告.
当客户端使用WMI自动启动远程进程时,它无法访问网络共享.但是,如果我使用远程桌面连接到远程计算机并手动启动后端进程,则会成功访问网络共享.
WMI调用中指定的用户和远程桌面会话登录的用户是相同的,因此权限应该相同,不是吗?
我在Directory.Exists()的MSDN条目中看到它指出"Exists方法不执行网络身份验证.如果您在未经过预先身份验证的情况下查询现有网络共享,则Exists方法将返回false." 我认为这是相关的?如何确保在WMI会话中正确验证用户身份?
ConnectionOptions opts = new ConnectionOptions();
opts.Username = username;
opts.Password = password;
ManagementPath path = new ManagementPath(string.Format("\\\\{0}\\root\\cimv2:Win32_Process", remoteHost));
ManagementScope scope = new ManagementScope(path, opts);
scope.Connect();
ObjectGetOptions getOpts = new ObjectGetOptions();
using (ManagementClass mngClass = new ManagementClass(scope, path, getOpts))
{
ManagementBaseObject inParams = mngClass.GetMethodParameters("Create");
inParams["CommandLine"] = commandLine;
ManagementBaseObject outParams = mngClass.InvokeMethod("Create", inParams, null);
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个涉及一些基本网络爬行的项目.我已经非常成功地使用HttpWebRequest和HttpWebResponse.对于cookie处理,我只有一个CookieContainer,我每次都分配给HttpWebRequest.CookieContainer.每次我自动填充新的cookie,不需要我的额外处理.直到不久之前,当其中一个曾经工作的网站突然停止工作时,这一切都很好.我有理由相信这是一个有问题的cookie,但是我没有保留过去工作时的cookie记录,所以我不是100%肯定.
我用以下代码设法模拟了这个问题:
CookieContainer cookieJar = new CookieContainer();
Uri uri1 = new Uri("http://www.somedomain.com/some/path/page1.html");
CookieCollection cookies1 = new CookieCollection();
cookies1.Add(new Cookie("NoPathCookie", "Page1Value"));
cookies1.Add(new Cookie("CookieWithPath", "Page1Value", "/some/path/"));
Uri uri2 = new Uri("http://www.somedomain.com/some/path/page2.html");
CookieCollection cookies2 = new CookieCollection();
cookies2.Add(new Cookie("NoPathCookie", "Page2Value"));
cookies2.Add(new Cookie("CookieWithPath", "Page2Value", "/some/path/"));
Uri uri3 = new Uri("http://www.somedomain.com/some/path/page3.html");
// Add the cookies from page1.html
cookieJar.Add(uri1, cookies1);
// Add the cookies from page2.html
cookieJar.Add(uri2, cookies2);
// We should now have 3 cookies
Console.WriteLine(string.Format("CookieJar contains {0} cookies", cookieJar.Count));
Console.WriteLine(string.Format("Cookies to send to page1.html: {0}", …Run Code Online (Sandbox Code Playgroud) 我使用FileVersionInfo.GetVersionInfo()得到一些严重的怪异,并希望有人可以提供帮助.
问题的基础是我正在遍历每个文件夹中调用GetVersionInfo()的所有文件.大约有300个文件.这适用于除2个文件之外的所有文件.对于这些DLL,我从GetVersionInfo()得到了完全不正确的信息.
为了消除所有其他变量,我将此调用提取到一个简单的测试应用程序中,它仍然遇到了同样的问题.但是,如果我将测试应用程序构建为Windows应用程序(最初是控制台应用程序),那么数据就会恢复正常.
只是为了澄清一下,当作为控制台应用程序运行时返回的不正确数据不仅仅是空信息,就像文件不包含版本数据一样.它包含合理的数据,但只包含错误的数据.就好像是从不同的文件中读取它一样.我找了一个包含匹配版本数据的文件,但找不到.
如果构建为控制台应用程序而不是Windows应用程序,为什么这个简单的调用功能会不同?
如果有人可以帮助我,我将非常感激.
安德,安迪
- 已添加代码
using System;
using System.Diagnostics;
namespace test
{
class Program
{
static void Main(string[] args)
{
string file = "C:\\ProblemFile.dll";
FileVersionInfo version = FileVersionInfo.GetVersionInfo(file);
string fileName = version.FileName;
string fileVersion = version.FileVersion;
Console.WriteLine(string.Format("{0} : {1}", fileName, fileVersion));
}
}
}
Run Code Online (Sandbox Code Playgroud) .net c# system.diagnostics console-application getfileversion