Gokul's Blog


Leave a comment

Source code formatting in wordpress

http://en.support.wordpress.com/code/posting-source-code/

[ sourcecode language="javascript"]
 // javascript hello world program
 alert('Hello, World !!');
[ /sourcecode ]

 
For C#: 
[ sourcecode language="csharp"]
<code>//...
[ /sourcecode]
Advertisements


Leave a comment

UNC access in C#

Below code helps to access a UNC path with different credentials

Thanks to source article: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/0cbd77b7-5c6c-4a86-bc3a-e06eb888fa17
Usage:
UNCAccess uncAccess = new UNCAccess(uncPath,”username”, “domain”,”Password”);

Code for UNCAccess class:

public class UNCAccess
{
	[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
	internal struct USE_INFO_2
	{
		internal LPWSTR ui2_local;
		internal LPWSTR ui2_remote;
		internal LPWSTR ui2_password;
		internal DWORD ui2_status;
		internal DWORD ui2_asg_type;
		internal DWORD ui2_refcount;
		internal DWORD ui2_usecount;
		internal LPWSTR ui2_username;
		internal LPWSTR ui2_domainname;
	}

	[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
	internal static extern NET_API_STATUS NetUseAdd(
		LPWSTR UncServerName,
		DWORD Level,
		ref USE_INFO_2 Buf,
		out DWORD ParmError);

	[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
	internal static extern NET_API_STATUS NetUseDel(
		LPWSTR UncServerName,
		LPWSTR UseName,
		DWORD ForceCond);

	private string sUNCPath;
	private string sUser;
	private string sPassword;
	private string sDomain;
	private int iLastError;
	public UNCAccess()
	{
	}
	public UNCAccess(string UNCPath, string User, string Domain, string Password)
	{
		login(UNCPath, User, Domain, Password);
	}
	public int LastError
	{
		get { return iLastError; }
	}

	///
<summary> /// Connects to a UNC share folder with credentials
 /// </summary>
	///UNC share path
	///Username
	///Domain
	///Password
	/// True if login was successful
	public bool login(string UNCPath, string User, string Domain, string Password)
	{
		sUNCPath = UNCPath;
		sUser = User;
		sPassword = Password;
		sDomain = Domain;
		return NetUseWithCredentials();
	}
	private bool NetUseWithCredentials()
	{
		uint returncode;
		try
		{
			USE_INFO_2 useinfo = new USE_INFO_2();

			useinfo.ui2_remote = sUNCPath;
			useinfo.ui2_username = sUser;
			useinfo.ui2_domainname = sDomain;
			useinfo.ui2_password = sPassword;
			useinfo.ui2_asg_type = 0;
			useinfo.ui2_usecount = 1;
			uint paramErrorIndex;
			returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
			iLastError = (int)returncode;
			return returncode == 0;
		}
		catch
		{
			iLastError = Marshal.GetLastWin32Error();
			return false;
		}
	}

	///
<summary> /// Closes the UNC share
 /// </summary>
	/// True if closing was successful
	public bool NetUseDelete()
	{
		uint returncode;
		try
		{
			returncode = NetUseDel(null, sUNCPath, 2);
			iLastError = (int)returncode;
			return (returncode == 0);
		}
		catch
		{
			iLastError = Marshal.GetLastWin32Error();
			return false;
		}
	}

}