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

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

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

Windows API enum and struct 


两个方法就比较简单了,一个用来连接,一个用来关闭。

    /// <summary>
    
/// 连接网络上的共享文件夹
    
/// </summary>

    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, 
11);
        }

    }
posted @ 2008-04-16 16:52  申龙斌的程序人生  阅读(10385)  评论(8编辑  收藏  举报