简单的远程上传问题请教各位!
客户的上级单位(不在一个城市)要求我们为其编写一个上传数据文件的C#程序,告知我上传地址为:\\100.110.120.155\AVS_FilePort\Input,用户名为:abcd,密码为:qaz123,说是让我从电脑的:开始-运行-敲入\\100.110.120.155\AVS_FilePort\Input,跳出输入用户名和密码窗口,选择记住密码。我觉得100.110.120.155应该是远程电脑的IP地址,AVS_FilePort应该是一个共享文件夹。我试着编写了以下代码:
string fileName1 = "ZFM1" + "_UPLOAD" + "_WEIGHT_" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xml";
string fileName2 = Application.StartupPath.ToString() + @"\xml\" + fileName1;
if (File.Exists(fileName2))//判断.xml文件是否存在
{
string to = @"\\100.110.120.155\AVS_FilePort\Input\" + fileName1;
string from = fileName2;
try
{
File.Copy(from, to, true);
MessageBox.Show("远程上传成功!");
}
catch (Exception ex)
{
MessageBox.Show("远程上传失败!");
}
}
由于本人从未搞过远程上传文件,想请教一下各位:
1. 所谓的"跳出输入用户名和密码窗口,选择记住密码"在程序中如何实现?
2. 客户告知的上传网址是:\\100.110.120.155\AVS_FilePort\Input,那我在代码中应该如上所写,还是应该写成:"http://100.110.120.155/AVS_FilePort/Input"这种格式呢?
谢谢指教!
------解决方案--------------------[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);
...
LogonUser(userName, domainName, password,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
out safeTokenHandle);
...
using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
{
using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
{
// Check the identity.
Console.WriteLine("After impersonation:&nbs