C#レコード、未整理
6307 ワード
//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;
}
}
}