关于局域网的共享的程序

首先一段是 检测程序是否联通

 public   class LanTestcs
{
    public static bool flag = false;

    [DllImport("Mpr.dll", EntryPoint = "WNetAddConnection2", CallingConvention = CallingConvention.Winapi)]
    private static extern int WNetAddConnection2(NETRESOURCE lpNetResource, string lpPassword, string lpUsername, System.UInt32 dwFlags);

    [StructLayout(LayoutKind.Sequential)]
    private class NETRESOURCE
    {
        public ResourceScope dwScope = 0;
        public ResourceType dwType = 0;
        public ResourceDisplayType dwDisplayType = 0;
        public ResourceUsage dwUsage = 0;
        public string lpLocalName = null;
        public string lpRemoteName = null;
        public string lpComment = null;
        public string lpProvider = null;
    };

    public enum ResourceScope
    {
        RESOURCE_CONNECTED = 1,
        RESOURCE_GLOBALNET,
        RESOURCE_REMEMBERED,
        RESOURCE_RECENT,
        RESOURCE_CONTEXT
    };

    public enum ResourceType
    {
        RESOURCETYPE_ANY,
        RESOURCETYPE_DISK,
        RESOURCETYPE_PRINT,
        RESOURCETYPE_RESERVED
    };

    public enum ResourceUsage
    {
        RESOURCEUSAGE_CONNECTABLE = 0x00000001,
        RESOURCEUSAGE_CONTAINER = 0x00000002,
        RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
        RESOURCEUSAGE_SIBLING = 0x00000008,
        RESOURCEUSAGE_ATTACHED = 0x00000010,
        RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
    };

    public enum ResourceDisplayType
    {
        RESOURCEDISPLAYTYPE_GENERIC,
        RESOURCEDISPLAYTYPE_DOMAIN,
        RESOURCEDISPLAYTYPE_SERVER,
        RESOURCEDISPLAYTYPE_SHARE,
        RESOURCEDISPLAYTYPE_FILE,
        RESOURCEDISPLAYTYPE_GROUP,
        RESOURCEDISPLAYTYPE_NETWORK,
        RESOURCEDISPLAYTYPE_ROOT,
        RESOURCEDISPLAYTYPE_SHAREADMIN,
        RESOURCEDISPLAYTYPE_DIRECTORY,
        RESOURCEDISPLAYTYPE_TREE,
        RESOURCEDISPLAYTYPE_NDSCONTAINER
    }

    public static int ConnectRemote(string shareName, string username, string password)//调取
    {
        NETRESOURCE nr = new NETRESOURCE();
        nr.dwType = ResourceType.RESOURCETYPE_DISK;
        nr.lpLocalName = null;
        nr.lpRemoteName = shareName;
        nr.lpProvider = null;

        int result = WNetAddConnection2(nr, password, username, 0);
        int OutResult = 0;
        if (result == 0 || result == 1219)//1219的状况是 没有以相同的身份登录
        {
            flag = true;
            OutResult = 0;//返回正常

        }
        else
        {
            flag = false;
            OutResult = 1;//返回非正常
        }
        return OutResult;
    }

    public static bool IsExist(string uri)
    {
        return Directory.Exists(uri);
        //HttpWebRequest req = null;
        //HttpWebResponse res = null;
        //try
        //{
        //    req = (HttpWebRequest)WebRequest.Create(@"\\192.168.1.102\项目共享");
        //    req.Method = "HEAD";
        //    req.Timeout = 100;
        //    res = (HttpWebResponse)req.GetResponse();
        //    return (res.StatusCode == HttpStatusCode.OK);
        //}
        //catch
        //{
        //    return false;
        //}
        //finally
        //{
        //    if (res != null)
        //    {
        //        res.Close();
        //        res = null;
        //    }
        //    if (req != null)
        //    {
        //        req.Abort();
        //        req = null;
        //    }
        //}
    }
}

共享文档上传下载方法也就是 File 的copy

  public  class NetFileUploadAndDownload
{
     

    /// <summary>
    /// 上传数据
    /// </summary>
    /// <param name="modelList"></param>
    /// <returns></returns>
    public bool UploadFile( string ServerDir,string  FilePath)
    {
        FileInfo info = new FileInfo(FilePath);
        string ServerFileName = ServerDir + "\\" + info.Name;
        bool flag = false;
        try
        {  
            File.Copy(FilePath, ServerFileName, true); 
                
            flag = true;
        }
        catch (Exception ee)
        {
             
            flag = false;
        } 
        return flag;

    }
    /// <summary>
    /// 文件存放在临时文件夹中 DownloadDir\\文件夹中
    /// </summary>
    /// <param name="ServerDir">服务器路径</param>
    /// <param name="FileName">文件名</param>
    /// <param name="DownloadDir">下载到指定文件夹('\\'结尾)</param>
    /// <returns></returns>
    public bool DownLoadFiles(string ServerDir, string FileName,string DownloadDir)
    {
        string ServerFileName = ServerDir + "\\" + FileName;
        bool flag = false; 
        if (!Directory.Exists(DownloadDir))
        {
            Directory.CreateDirectory(DownloadDir);
        }
        string FilePath = DownloadDir + FileName;
        try
        { 
            File.Copy(ServerFileName, FilePath, true);
            flag = true;
        }
        catch (Exception ee)
        {

            flag = false;
        } 
        return flag; 
    }


}

具体的使用方法是:
检测局域网是否有权限:
if (LanTestcs.ConnectRemote(FileDir, UserName, Pwd)== 0)
使用上传的方法:
NetFileUploadAndDownload UpAndDown = new NetFileUploadAndDownload();
(UpAndDown.DownLoadFiles(ServerDir, fileModel.FileName,GModel.TempFile))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值