我正在开发一个控制台应用程序,该应用程序尝试通过登录然后将文件复制到共享文件夹来从网站下载一些文件。
我在这里工作的域与我需要保存文件的共享文件夹的域不同。
目前,我通过从运行中打开共享文件夹并将用户名和密码放入 Windows 身份验证对话框然后运行应用程序来手动克服此问题。
这是我正在使用的代码 -
static public void Start()
{
try
{
serverpath = ConfigurationSettings.AppSettings["serverpath"];
if (flag == 0)
{
username = ConfigurationSettings.AppSettings["UserNameFN"];
password = ConfigurationSettings.AppSettings["PassWordFN"];
}
else
{
username = ConfigurationSettings.AppSettings["UserNameMFS"];
password = ConfigurationSettings.AppSettings["PassWordMFS"];
check = true;
}
string webUrl = "https://www.website.org/";
string formParams = string.Format("user={0}&password={1}", username, password);
WebRequest req = WebRequest.Create(webUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.Proxy.Credentials = CredentialCache.DefaultCredentials;
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
(ConfigurationSettings.AppSettings["UserNamePROD"], ConfigurationSettings.AppSettings["PassWordPROD"], ConfigurationSettings.AppSettings["DomainPROD"]);
Directory.CreateDirectory(serverpath + @"Log\");
Logging.Write_Line(DateTime.Now + " - File Transfer Started");
if ((!check) || (count == 0))
{
string[] filename = getFileNames.returnFileNames(cookieHeader)
}
}
有了上面的代码
Directory.CreateDirectory(serverpath + @"Log\");
此行引发异常。
在我的机器上运行我的应用程序时,如何以编程方式登录到共享位置?
提前致谢 :)