您的位置:首页 > 其它

访问远程服务器上的共享文件夹

2008-04-16 16:52 363 查看
春季论证会逼近,地震数据体不断增加,硬盘空间吃紧,只能将一部分数据体放到187上来解决了。
由于抽取地震剖面的程序是在WEB Server中调用的,在Windows中访问187上的共享目录正常,但在程序中好像访问不到共享目录。只能在C#程序中来访问共享目录了,从网上找了一段代码改了改,很快就可以用了,只是没有进行异常处理。

程序中要用到Windows API的函数,需要定义一些枚举变量。

public class NetworkSharedFolder
{

[DllImport("mpr.dll")]
public static extern int WNetAddConnection2A(NETRESOURCE[] lpNetResource, string lpPassword, string lpUserName, int dwFlags);

[DllImport("mpr.dll")]
public static extern int WNetCancelConnection2A(string sharename, int dwFlags, int fForce);

/**//// <summary>
///
/// </summary>
/// <param name="remotePath"></param>
/// <param name="localPath"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
public static int Connect(string remotePath, string localPath, string username, string password)
{
NETRESOURCE[] share_driver = new NETRESOURCE[1];
share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_DISK;
share_driver[0].lpLocalName = localPath;
share_driver[0].lpRemoteName = remotePath;

Disconnect(localPath);
int ret = WNetAddConnection2A(share_driver, password, username, 1);

return ret;
}

public static void Disconnect(string localpath)
{
WNetCancelConnection2A(localpath, 1, 1);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: