<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Phanix's Blog &#187; network drive</title>
	<atom:link href="http://blog.phanix.idv.tw/archives/tag/network-drive/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.phanix.idv.tw</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 01:03:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C# mount / unmount UNC (network folder / drive) 連結網路磁碟機</title>
		<link>http://blog.phanix.idv.tw/archives/2009/09/08/720/</link>
		<comments>http://blog.phanix.idv.tw/archives/2009/09/08/720/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 02:10:54 +0000</pubDate>
		<dc:creator>Phanix</dc:creator>
				<category><![CDATA[學習工作]]></category>
		<category><![CDATA[工作]]></category>
		<category><![CDATA[程式]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[network drive]]></category>
		<category><![CDATA[network folder]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[UNC]]></category>
		<category><![CDATA[網路磁碟機]]></category>

		<guid isPermaLink="false">http://blog.phanix.idv.tw/archives/2009/09/08/720/</guid>
		<description><![CDATA[有些時候為了 security 問題或者想要詳細紀錄使用者存取網路磁碟機或目錄中資料的情況, 所以會另外建立帳號並透過程式來存取網路磁碟機, 而不讓使用者直接用他的帳號密碼來存取, 這時候就需要讓程式可以建立網路磁碟機與中斷連線. 我自己比較喜歡第一個方式, 相對來說簡單許多. Approach 1 using IWshRuntimeLibrary; //Add "Windows Script Host Object Model" COM reference private WshNetwork _networkShell = new WshNetwork(); //UNC is the network folder/drive path, e.g. \\server\folder1 public void MapDrive(string driveLetter, string UNC, bool isPersistent, string userName, string password) { // Disconnect the drive first, forcing a permanent [...]]]></description>
			<content:encoded><![CDATA[<p>有些時候為了 security 問題或者想要詳細紀錄使用者存取網路磁碟機或目錄中資料的情況, 所以會另外建立帳號並透過程式來存取網路磁碟機, 而不讓使用者直接用他的帳號密碼來存取, 這時候就需要讓程式可以建立網路磁碟機與中斷連線.</p>
<p>我自己比較喜歡第一個方式, 相對來說簡單許多.<br />
<span id="more-720"></span></p>
<h2>Approach 1</h2>
<pre style="font-size:11px; font-family:Times New Roman;">using IWshRuntimeLibrary;	//Add "Windows Script Host Object Model" COM reference

private WshNetwork _networkShell = new WshNetwork();

//UNC is the network folder/drive path, e.g. \\server\folder1
public void MapDrive(string driveLetter, string UNC, bool isPersistent, string userName, string password)
{
	// Disconnect the drive first, forcing a permanent change.
	if (driveLetter != "")
	{
		DisconnectDrive(driveLetter, true, true);
	}
	else
	{
		DisconnectDrive(UNC, true, true);
	}

	// Map the drive to the path.
	object persistent = isPersistent;
	object user = userName;
	object pwd = password;

	_networkShell.MapNetworkDrive(driveLetter, UNC, ref persistent, ref user, ref pwd);
}

public void DisconnectDrive(string UNC_or_DriveName, bool willForce, bool isPersistent)
{
	try
	{
		object force = willForce;
		object updateProfile = isPersistent;

		_networkShell.RemoveNetworkDrive(UNC_or_DriveName, ref force, ref updateProfile);
	}
	catch
	{
	}
}
</pre>
<h2>Approach 2</h2>
<pre style="font-size:11px; font-family:Times New Roman;">using BOOL = System.Boolean;
using DWORD = System.UInt32;
using LPWSTR = System.String;
using NET_API_STATUS = System.UInt32;

[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);

[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;
}

private void Mount(string drive, string networkPath, string id, string pw)
{
	if (drive != "")
	{
		NetUseDel("", drive, 2);
	}
	else
	{
		NetUseDel("", networkPath, 2);
	}

	USE_INFO_2 useInfo = new USE_INFO_2();
	useInfo.ui2_local = drive;
	useInfo.ui2_remote = networkPath;
	useInfo.ui2_password = pw;
	useInfo.ui2_asg_type = 0;    //disk drive
	useInfo.ui2_usecount = 1;
	useInfo.ui2_username = id;
	//useInfo.ui2_domainname = "";

	uint paramErrorIndex;
	uint returnCode = NetUseAdd(null, 2, ref useInfo, out paramErrorIndex);
	if (returnCode != 0)
	{
		throw new Win32Exception((int)returnCode);
	}
}

private void UnMount(string networkPath_or_Drive)
{
	NetUseDel("", networkPath_or_Drive, 2);
}
</pre>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7434619175264093";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p> ]]></content:encoded>
			<wfw:commentRss>http://blog.phanix.idv.tw/archives/2009/09/08/720/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

