日期:2014-05-18  浏览次数:20941 次

C# 网盘映射问题(分不多了,就100)
写了个程序,就是实现网络共享文件夹和本地的文件夹映射的
网络映射的代码
C# code
[DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
        public static extern uint WNetAddConnection2(
            [In] NETRESOURCE lpNetResource,
            string lpPassword,
            string lpUsername,
            uint dwFlags);

        [DllImport("Mpr.dll")]
        public static extern uint WNetCancelConnection2(
            string lpName,
            uint dwFlags,
            bool fForce);

        [StructLayout(LayoutKind.Sequential)]
        public class NETRESOURCE
        {
            public int dwScope;
            public int dwType;
            public int dwDispalyType;
            public int dwUsage;
            public string LocalName;
            public string RemoteName;
            public string Comment;
            public string Provider;
        }

        public override int MappingDrive(string localname, string path,string provider)
        {
            try
            {
                NETRESOURCE myNetResource = new NETRESOURCE();
                myNetResource.dwScope = 2;
                myNetResource.dwType = 1;
                myNetResource.dwDispalyType = 3;
                myNetResource.dwUsage = 1;
                myNetResource.LocalName = localname;
                myNetResource.RemoteName = path;

                uint nret = WNetAddConnection2(myNetResource, "9fcs999", "jianbo", 0);

                return Convert.ToInt32(nret);
            }
            catch
            {
                return -1;
            }
        }

        public override int CloseDrive(string localname)
        {
            uint cnret = WNetCancelConnection2(localname, 0, true);
            Thread.Sleep(1000);
            return Convert.ToInt32(cnret);
        }


本地映射的代码
C# code

public override int MappingDrive(string localname, string path,string provider)
        {
            string para = localname + " " + path;
            StringBuilder outputStr = new StringBuilder();
            int re = 1;
            try
            {
                Microsoft.Win32.RegistryKey key;
                key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"software\microsoft\PCHealth\ErrorReporting\", true);
                int doReport = (int)key.GetValue("DoReport");

                if (doReport != 0)
                {
                    key.SetValue("DoReport", 0);
                }

                int showUI = (int)key.GetValue("ShowUI");
                if (showUI != 0)
                {
                    key.SetValue("ShowUI", 0);
                }
            }
            catch
            {
            }

            m_Error = "";
            m_Output = "";
            try
            {
                Process p = new Process();
                p.StartInfo.FileName = "subst"; // fileName;
                p.StartInfo.Arguments = para;
                p.StartInfo.UseShellExecute = false;
                //p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                p.WaitForExit();
                var exitCode = p.ExitCode;
                re = exitCode;
            }
            catch (Exception e)
            {
                m_Error = e.Message;
            }

            return re;
        }

        public override int CloseDrive(string localname)
        {
            int re = MappingDrive(localname, " /D