将文件从另一个用户和域写入位置 [英] write files to a location from another user and domain
问题描述
我正在开发一个控制台应用程序,该应用程序尝试通过登录从网站下载一些文件,然后将这些文件复制到共享文件夹中.
I m working on a console application that tries to download some files from a website by logging in and then copying the files to a shared folder.
这里我使用的域与我需要保存文件的共享文件夹的域不同.
Here the domain I am working in is different that the domain of that shared folder where I need to save the files.
当前,我通过运行时打开共享文件夹并将用户名和密码放入Windows身份验证对话框中,然后运行应用程序来手动解决此问题.
Currently I overcome this problem manually by opening the shared folder from the run and putting the username and password into the windows authentication dialog and then running the application.
这是我正在使用的代码-
Here is the code I m using -
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\");
此行引发异常.
从计算机上运行应用程序时,如何以编程方式登录共享位置?
How can I programmatically login to the shared location while running my application from my machine?
先谢谢您了:)
推荐答案
您可以通过将目录创建命令作为Shell进程执行来避免使用它,像这样(提供完整的名称空间,以避免使用
You might get away with it by executing the directory creation command as a shell process, like this (full namespaces provided in order to avoid cluttering up code with using
directives):
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo =
new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // Hides the command prompt window
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C mkdir " + serverpath + @"\Log";
startInfo.Username = "domain\username";
startInfo.Password = // SecureString object containing the password
process.StartInfo = startInfo;
process.Start();
未经测试,但逻辑表明它可能有效.
Not tested but logic suggests it might work.
这篇关于将文件从另一个用户和域写入位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!