Heres the code, thanks,
-----------------------------------------------------------
using System;
using Microsoft.Win32;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
struct USER_INFO_1003
{
public string usri1003_password;
}
[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCEA
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
[ MarshalAs (UnmanagedType.LPStr)] public string lpLocalName;
[ MarshalAs (UnmanagedType.LPStr)] public string lpRemoteName;
[ MarshalAs (UnmanagedType.LPStr)] public string lpComment;
[ MarshalAs (UnmanagedType.LPStr)] public string lpProvider;
public override String ToString()
{
String str = "LocalName: " + lpLocalName + " RemoteName: " + lpRemoteName
+ " Comment: " + lpComment + " lpProvider: " + lpProvider;
return(str);
}
}
namespace wnetaddconnection
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2(
[MarshalAs(UnmanagedType.LPArray)] NETRESOURCEA[] lpNetResource,
[MarshalAs(UnmanagedType.LPStr)] string lpPassword,
[MarshalAs(UnmanagedType.LPStr)] string UserName,
int dwFlags);
[DllImport("mpr.dll")]
extern static int WNetCancelConnection2(
[MarshalAs(UnmanagedType.LPWStr)] string lpName,
int dwFlags,
int fForce
);
[DllImport("Netapi32.dll")]
extern static int NetUserSetInfo(
[MarshalAs(UnmanagedType.LPWStr)] string servername,
[MarshalAs(UnmanagedType.LPWStr)] string username,
int level,
ref USER_INFO_1003 buf,
int error);
[STAThread]
static void Main(string[] args)
{
string administrator="Administrator";
string password="password1";
string target="192.168.88.100";
string lpName=@"\\" + target + @"\IPC$";
string username="fred";
string newpassword="hellomum";
NETRESOURCEA [] n = new NETRESOURCEA[1];
n[0] = new NETRESOURCEA();
n[0].dwScope = 2;
n[0].dwType = 0;
int dwFlags = 1;
n[0].lpLocalName = null;
n[0].lpRemoteName = lpName;
Console.WriteLine(n[0]);
try
{
// Add an IPC$ connection to the remote host
int res = WNetAddConnection2( n, password, administrator, dwFlags );
Console.WriteLine("WNetAddConnection2 returned : " + res);
USER_INFO_1003 UserInfo1003 = new USER_INFO_1003();
UserInfo1003.usri1003_password=newpassword;
// Set the password of a user
res = NetUserSetInfo(
target,
username,
1003,
ref UserInfo1003,
0);
// Cancel the connection
res = WNetCancelConnection2(lpName, 1, 0);
Console.WriteLine("WNetCancelConnection2 returned : " + res);
}
catch (Exception e)
{
Console.WriteLine(e.Message + " hit return ..");
Console.ReadLine();
}
Console.WriteLine("hit return ..");
Console.ReadLine();
}
}
}