none
有关Vista共享文件夹的问题 RRS feed

  • 问题

  • Vista访问另一台主机的共享目录为什么总是要输入用户名密码呢?我现在是在C#程序里用Process.Start()来打开共享文件夹,不知道该怎么输用户名和密码,结果程序老是出错。现在我是通过传命令给客户端,客户端直接调net share来设定一个共享文件夹,控制端并不知道客户机的用户名和密码。该怎么办?请高手指点!谢谢~

    2008年7月17日 14:34

答案

  • 参考如下代码来实现,可以设置访问共享的用户名和密码:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Runtime.InteropServices;  


    /// <summary>
    /// useNet 的摘要说明
    /// </summary>
    public class useNet
    {
    public useNet()
    {
       //
       // TODO: 在此处添加构造函数逻辑
       //
    }
    //declare   WindowsNetwork   function  
    //cancel   net   mapped  
    [DllImport("mpr.dll")]  
    public   static   extern   WNetCancelConnection2ReturnCode   WNetCancelConnection2(  
    string   strResourceName,  
    ConnectionType   connectionType,  
    bool   bForce  
    );  
    //builde   net   mapped  
    [DllImport("mpr.dll")]  
    public   static   extern   WNetAddConnection2ReturnCode   WNetAddConnection2(  
    ref   NetResource   netResource,  
    string   strPassword,  
    string   strUserName,  
    ConnectionOption   dwFlags  
    );  
    }  
       
    //ConnectionType  
    public   enum   ConnectionType  
    {  
    CONNECT_DONT_UPDATE_PROFILE=0,  
    CONNECT_UPDATE_PROFILE=0x00000001,  
    CONNECT_UPDATE_RECENT=0x00000002,  
    CONNECT_TEMPORARY=0x00000004,  
    CONNECT_INTERACTIVE=0x00000008,  
    CONNECT_PROMPT=0x00000010,  
    CONNECT_NEED_DRIVE=0x00000020,  
    CONNECT_REFCOUNT=0x00000040,  
    CONNECT_REDIRECT=0x00000080,  
    CONNECT_LOCALDRIVE=0x00000100,  
    CONNECT_CURRENT_MEDIA=0x00000200,  
    CONNECT_DEFERRED=0x00000400,  
    CONNECT_RESERVED=unchecked((int)0xFF000000)  
    }  
       
    //WNetCancelConnection2  
    public   enum   WNetCancelConnection2ReturnCode  
    {  
    NO_ERROR=0,    
    ERROR_BAD_PROFILE=1206,    
    ERROR_CANNOT_OPEN_PROFILE=1205,    
    ERROR_DEVICE_IN_USE=2404,      
    ERROR_EXTENDED_ERROR=1208,    
    ERROR_NOT_CONNECTED=2250,    
    ERROR_OPEN_FILES=2401    
    }  
       
    //WNetAddConnection2  
    public   enum   WNetAddConnection2ReturnCode  
    {  
    NO_ERROR=0,    
    ERROR_ACCESS_DENIED=5,    
    ERROR_ALREADY_ASSIGNED=85,    
    ERROR_BAD_DEV_TYPE=66,    
    ERROR_BAD_DEVICE=1200,    
    ERROR_BAD_NET_NAME=67,    
    ERROR_BAD_PROFILE=1206,    
    ERROR_BAD_PROVIDER=1204,    
    ERROR_BUSY=170,    
    ERROR_CANCELLED=1223,    
    ERROR_CANNOT_OPEN_PROFILE=1205,  
    ERROR_DEVICE_ALREADY_REMEMBERED=1202,    
    ERROR_EXTENDED_ERROR=1208,    
    ERROR_INVALID_PASSWORD=86,    
    ERROR_NO_NET_OR_BAD_PATH=1203,    
    ERROR_NO_NETWORK=1222  
    }  
    //NETRESOURCE   struc:  
    public   struct   NetResource  
    {    
    public   ResourceEnumerationScope   dwScope;    
    public   ResourceType   dwType;    
    public   ResourceDisplayType   dwDisplayType;    
    public   ResourceUsage   dwUsage;    
    public   string   lpLocalName;    
    public   string   lpRemoteName;    
    public   string   lpComment;    
    public   string   lpProvider;    
    }  
       
    //ResourceType   enum:    
    public   enum   ResourceType  
    {  
    RESOURCETYPE_ANY=0x00000000,    
    RESOURCETYPE_DISK=0x00000001,    
    RESOURCETYPE_PRINT=0x00000002    
    }
    public enum ResourceEnumerationScope
    {
        RESOURCE_CONNECTED = 0x00000001,
        RESOURCE_GLOBALNET = 0x00000002,
        RESOURCE_REMEMBERED = 0x00000003
    }  

       
    //ResourceDisplayType   enum:    
    public   enum   ResourceDisplayType  
    {  
    RESOURCEDISPLAYTYPE_DOMAIN=0x00000001,   RESOURCEDISPLAYTYPE_SERVER=0x00000002,   RESOURCEDISPLAYTYPE_SHARE=0x00000003,   RESOURCEDISPLAYTYPE_GENERIC=0x00000000    
    }  
       
    //ResourceUsage   enum:    
    public   enum   ResourceUsage  
    {  
    RESOURCEUSAGE_CONNECTABLE=0x00000001,    
    RESOURCEUSAGE_CONTAINER=0x00000002    
    }  
       
    //ConnectionOption   enum:    
    public   enum   ConnectionOption  
    {  
    CONNECT_INTERACTIVE=0x00000008,    
    CONNECT_PROMPT=0x00000010,    
    CONNECT_REDIRECT=0x00000080,    
    CONNECT_UPDATE_PROFILE=0x00000001,    
    CONNECT_COMMANDLINE=0x00000800,  
    CONNECT_CMD_SAVECRED=0x00001000    
    }  

    //运用
            NetResource nr;
            nr.dwDisplayType = ResourceDisplayType.RESOURCEDISPLAYTYPE_GENERIC;
            nr.dwScope = ResourceEnumerationScope.RESOURCE_GLOBALNET;
            nr.dwType = ResourceType.RESOURCETYPE_DISK;
            nr.dwUsage = ResourceUsage.RESOURCEUSAGE_CONNECTABLE;
            nr.lpComment = "";
            nr.lpLocalName = "z:";
            nr.lpProvider = "";
            nr.lpRemoteName = @"\\192.168.1.254\softwares";

            if (useNet.WNetAddConnection2(ref   nr, "密码", "用户名", ConnectionOption.CONNECT_INTERACTIVE) == WNetAddConnection2ReturnCode.NO_ERROR)
            {
                Response.Write("mapped   succeed");
            }
            else
            {
                Response.Write("mapped   failure");
            }

            string straimFile = "z:" + @"\";
            DirectoryInfo dir = new DirectoryInfo(straimFile);
            DirectoryInfo[] fil = dir.GetDirectories();
            for (int i = 0; i < fil.Length; i++)
            {
                Response.Write(filIdea.ToString() + "<br>");
            }

            if (useNet.WNetCancelConnection2("z:", 0, true) == WNetCancelConnection2ReturnCode.NO_ERROR)
            {
                Response.Write("cancel   succeed");
            }
            else
            {
                Response.Write("cancel   error");
            }

    2008年7月17日 17:46
    版主