C# 记录,未整理

//1、执行命令  
Process.start("CMD.EXT", "/c  adb reboot")
var ps = Process.start("cmd.exe", "/c adb reboot")
ps.WaitForExit

//2、Samba磁盘映射
public static string MapDrive (string url, string user, string pwd)
{

	String cmdString = "net use " + url + " /user:" + user + " " + pwd;

	ManagementClass processClass = new ManagementClass("Win32_Process");

	object[] methodArgs = { cmdString, null, null, 0 };

	object result = processClass.InvokeMethod("Create", methodArgs);

	return result.ToString();

}
//返回值值未完整的路径。类似于本地的磁盘。可以直接调用File进行读写
//删除映射未实现


//3、通过WMI实现远程创建共享目录,远程拷贝文件等操作
//   http://www.cnblogs.com/ajiefj/archive/2010/05/11/1732963.html

//4、调用Win32 API实现磁盘映射
//参考 http://blog.sina.com.cn/s/blog_3d73810501012mxl.html

//资源类,用于记录映射类型和设置 
[StructLayout(LayoutKind.Sequential)] 
public class NETRESOURCE 
{ 
    public int dwScope;//只能取2 
    public int dwType;//0为打印机或驱动器,1为驱动器,2为打印机 
    public int dwDisplayType;//取0,自动设置 
    public int dwUsage;//取1 
    public string LocalName;//本地盘符或名称 
    public string RemoteName;//远程地址 
    public string Comment;//NULL即可,A pointer to a NULL-terminated string that contains a comment supplied by the network provider. 
    public string Provider;//NULL即可,A pointer to a NULL-terminated string that contains the name of the provider that owns the resource. This member can be NULL if the provider name is unknown. 
} 
 
//控制(主)类,创建、删除映射 
public class NetDriveCtl 
{ 
    ArrayList NDList; 
 
    public NetDriveCtl() 
    { 
        NDList = new ArrayList(); 
    } 
 
    public string CreateDrive(string LocalName, string RemoteName, string UserName, string Password) 
    { 
        NETRESOURCE NetDrive = new NETRESOURCE(); 
        NetDrive.dwScope = 2; 
        NetDrive.dwType = 0; 
        NetDrive.dwDisplayType = 0; 
        NetDrive.dwUsage = 1; 
        NetDrive.LocalName = LocalName; 
        NetDrive.RemoteName = RemoteName; 
 
        NDList.Add(NetDrive); 
        return ConnectDrive(NetDrive, UserName, Password); 
    } 
 
    public Boolean DeleteDrive(string LocalName, string RemoteName) 
    { 
        foreach (NETRESOURCE NetDrive in NDList) 
        { 
            if ((NetDrive.LocalName == LocalName) && (NetDrive.RemoteName == RemoteName)) 
            { 
                DisconnectDrive(NetDrive); 
                NDList.Remove(NetDrive); 
                return true; 
            } 
        } 
        return false; 
    } 
 
    private string ConnectDrive(NETRESOURCE NetDrive, string UserName, string Password) 
    { 
        StringBuilder UN = new StringBuilder(UserName); 
        StringBuilder PW = new StringBuilder(Password); 
 
        return WNetAddConnection2(NetDrive, PW, UN, 0).ToString(); 
    } 
 
    private string DisconnectDrive(NETRESOURCE NetDrive) 
    { 
        string LocalName = NetDrive.LocalName; 
        return WNetCancelConnection2(LocalName, 1, true).ToString(); 
    } 
 
    private string DisconnectDrive(string LocalName) 
    { 
        return WNetCancelConnection2(LocalName, 1, true).ToString(); 
    } 
 
    //这两个是系统API函数 
    [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")] 
    private static extern uint WNetAddConnection2([In] NETRESOURCE lpNetResource, StringBuilder lpPassword, StringBuilder lpUsername, uint dwFlags); 
    [DllImport("Mpr.dll")] 
    private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce); 
} 

//调用例程序
NetDriveCtl ndc = new NetDriveCtl(); 
ndc.CreateDrive("T:", @"\\1.1.1.1\V$", "password", "username"); 


//5、C# 如何获取映射网络驱动器的共享路径
//参考链接  http://blog.csdn.net/sabty/article/details/4792617

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Xml;
namespace PathingDemos
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = Pathing.GetUNCPath(@"Z:/"); // path = @"//192.168.1.2/共享"
        }
    }
    public static class Pathing
    {
        [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern int WNetGetConnection(
            [MarshalAs(UnmanagedType.LPTStr)] string localName,
            [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName,
            ref int length);
        /// <summary>
        /// 给定一个路径,返回的网络路径或原始路径。 
        /// 例如:给定路径 P:/2008年2月29日(P:为映射的网络驱动器名),可能会返回:“//networkserver/照片/2008年2月9日”
        /// </summary>
        /// <param name="originalPath">指定的路径</param>
        /// <returns>如果是本地路径,返回值与传入参数值一样;如果是本地映射的网络驱动器</returns>
        public static string GetUNCPath(string originalPath)
        {
            StringBuilder sb = new StringBuilder(512<mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"></mce:script>);
            int size = sb.Capacity;
            if (originalPath.Length > 2 && originalPath[1] == ':')
            {
                char c = originalPath[0];
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                {
                    int error = WNetGetConnection(originalPath.Substring(0, 2),
                        sb, ref size);
                    if (error == 0)
                    {
                        DirectoryInfo dir = new DirectoryInfo(originalPath);
                        string path = Path.GetFullPath(originalPath)
                            .Substring(Path.GetPathRoot(originalPath).Length);
                        return Path.Combine(sb.ToString().TrimEnd(), path);
                    }
                }
            }
            return originalPath;
        }
    }
}





你可能感兴趣的:(C# 记录,未整理)