创建一个带有按钮的简单表单,在单击事件上,您可以尝试以下代码。您可以使用此技术来更新与您要更新的软件相关的任何文件。
[DllImport("advapi32.DLL", SetLastError = true)]
public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
private void button1_Click(object sender, EventArgs e)
{
IntPtr admin_token = default(IntPtr);
WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
WindowsIdentity wid_admin = null;
WindowsImpersonationContext wic = null;
try
{
if (LogonUser("username", "domain", "password", 9, 0, ref admin_token) != 0)
{
wid_admin = new WindowsIdentity(admin_token);
wic = wid_admin.Impersonate();
System.IO.File.Copy("D:\\test.txt", "\\\\\\192.168.25.40\\c$\\D:\\test.txt", true);
MessageBox.Show("Copy succeeded");
}
else
{
MessageBox.Show("Copy Failed");
}
}
catch (System.Exception se)
{
int ret = Marshal.GetLastWin32Error();
MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString());
MessageBox.Show(se.Message);
}
finally
{
if (wic != null)
{
wic.Undo();
}
}
}