using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// hyNetUse 的摘要描述
/// </summary>
public class hyNetUse
{
    private string _ErrMsg = "";


    public bool ConnectToRemote(string FullPath)
    {
        return ConnectToRemote(FullPath, "", "", "");
    }
    public bool ConnectToRemote(string FullPath, string Domain)
    {
        return ConnectToRemote(FullPath, Domain, "", "");
    }
    public bool ConnectToRemote(string FullPath, string User, string Pwd)
    {
        return ConnectToRemote(FullPath, "", User, Pwd);
    }
    public bool ConnectToRemote(string FullPath, string Domain, string User, string Pwd)
    {
        return EstablishUNC(FullPath, Domain, User, Pwd);
    }

    public bool EstablishUNC(string FullPath, string Domain, string User, string Pwd)
    {
        FullPath = FullPath.Trim();
        if (FullPath.Substring(0, 2) != "\\\\")
            throw new Exception("Invalid remote path");

        if (FullPath.Substring(FullPath.Length - 1, 1) == "\\")
            FullPath = FullPath.Substring(0, FullPath.Length - 2);
        if (Domain.Length > 0)
            Domain += "\\";

        System.Diagnostics.Process p = null;
        System.Diagnostics.ProcessStartInfo pi = null;
        try
        {
            p = new System.Diagnostics.Process();

            // 其中 /C 為執行完命令後關閉視窗, /K 為執行完命令後不關閉視窗
            if (User.Length > 0)
                pi = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/C net use \"" + FullPath + "\" " + Pwd + " /user:\"" + Domain + User + "\"");
            else
                pi = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/C net use \"" + FullPath + "\"");

            pi.UseShellExecute = false;
            pi.RedirectStandardInput = true;
            pi.RedirectStandardOutput = true;
            pi.RedirectStandardError = true;
            pi.CreateNoWindow = true;
            p = System.Diagnostics.Process.Start(pi);
            p.EnableRaisingEvents = true;
            // 如果呼叫時沒有使用 /C 引數, 則 p.WaitForExit(5000) 會等待 5 秒至 timeout, 而呼叫 p.ExitCode 就會錯
            p.WaitForExit(5000);
            int iExitCode = p.ExitCode;

            if (iExitCode != 0)
                _ErrMsg = p.StandardError.ReadToEnd();
            p.Close();

            if (iExitCode != 0)
            {
                //throw Root.RootException.APCException(
                //    new Exception(_ErrMsg),
                //    "ExitCode=" + iExitCode);
                return false;
            }
            else
                return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            //p.Dispose();
            p.Close();
            p.Dispose();
            GC.Collect();
        }
    }


    public bool DestoryUNC(string DriveOrPath)
    {
        // driver
        if (DriveOrPath.IndexOf(":") > 0)
        {
            //
        }
        // unc path
        else if (DriveOrPath.Trim().IndexOf(@"\\") == 0)
        {
            if (DriveOrPath.Substring(DriveOrPath.Length - 1, 1) == "\\")
                DriveOrPath = DriveOrPath.Substring(0, DriveOrPath.Length - 2);
        }

        System.Diagnostics.Process p = null;
        System.Diagnostics.ProcessStartInfo pi = null;
        try
        {
            p = new System.Diagnostics.Process();
            pi = null;

            // 其中 /C 為執行完命令後關閉視窗, /K 為執行完命令後不關閉視窗
            pi = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/C net use \"" + DriveOrPath + "\" /DELETE /YES");

            pi.UseShellExecute = false;
            pi.RedirectStandardInput = true;
            pi.RedirectStandardOutput = true;
            pi.RedirectStandardError = true;
            pi.CreateNoWindow = true;
            p = System.Diagnostics.Process.Start(pi);
            p.EnableRaisingEvents = true;
            // 如果呼叫時沒有使用 /C 引數, 則 p.WaitForExit(5000) 會等待 5 秒至 timeout, 而呼叫 p.ExitCode 就會錯
            p.WaitForExit(5000);
            int iExitCode = p.ExitCode;

            if (iExitCode != 0)
                _ErrMsg = p.StandardError.ReadToEnd();
            p.Close();

            if (iExitCode != 0)
            {
                // 可能找不到網路時 (沒被連結時則不回報錯誤)

                //throw Root.RootException.APCException(
                //    new Exception(_ErrMsg),
                //    "ExitCode=" + iExitCode);
                return false;
            }
            else
                return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            p.Close();
            p.Dispose();
            GC.Collect();
        }
    }
    //public void DisconnectMappingDrive(string Driver) {
    //    using (System.Diagnostics.Process p = new System.Diagnostics.Process()) {
    //        p.StartInfo.FileName = "cmd.exe";
    //        p.StartInfo.Arguments = "net use " + Driver + " /delete";
    //        p.StartInfo.UseShellExecute = false;
    //        p.StartInfo.CreateNoWindow = true;
    //        p.Start();

    //        p.Close();
    //    }
    //}
}
