CarotDav_decompile/CarotDAV/NetDriveManager.cs

376 lines
15 KiB
C#

// Decompiled with JetBrains decompiler
// Type: CarotDAV.NetDriveManager
// Assembly: CarotDAV, Version=1.13.2.18337, Culture=neutral, PublicKeyToken=null
// MVID: C31F2651-A4A8-4D09-916A-8C6106F5E7C8
// Assembly location: F:\Eigene Dateien\Dropbox\portable Collection\Progs\CarotDAV\CarotDAV.exe
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace CarotDAV
{
public class NetDriveManager
{
private const uint SHGFI_ICON = 256;
private const uint SHGFI_LARGEICON = 0;
private const uint SHGFI_SMALLICON = 1;
private const uint SHGFI_ICONLOCATION = 4096;
public const string WebClientParameterRegPath = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\WebClient\\Parameters";
public const string WebClientFileSizeLimitRegKey = "FileSizeLimitInBytes";
public const int CONNECT_UPDATE_PROFILE = 1;
public const int NO_ERROR = 0;
public const int ERROR_ACCESS_DENIED = 5;
public const int ERROR_ALREADY_ASSIGNED = 85;
public const int ERROR_BAD_DEV_TYPE = 66;
public const int ERROR_BAD_DEVICE = 1200;
public const int ERROR_BAD_NET_NAME = 67;
public const int ERROR_BAD_NETPATH = 53;
public const int ERROR_BAD_PROFILE = 1206;
public const int ERROR_BAD_PROVIDER = 1204;
public const int ERROR_BUSY = 170;
public const int ERROR_CANCELLED = 1223;
public const int ERROR_CANNOT_OPEN_PROFILE = 1205;
public const int ERROR_DEVICE_ALREADY_REMEMBERED = 1202;
public const int ERROR_EXTENDED_ERROR = 1208;
public const int ERROR_INVALID_PASSWORD = 86;
public const int ERROR_NO_NET_OR_BAD_PATH = 1203;
public const int ERROR_NOT_CONNECTED = 2250;
private const int ssfNETHOOD = 19;
[DllImport("shell32.dll")]
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref NetDriveManager.SHFILEINFO psfi, int cbSizeFileInfo, uint uFlags);
[DllImport("Shell32.dll")]
private static extern void SHUpdateImage(string pszHashItem, int iIndex, int uFlags, int iImageIndex);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool DestroyIcon(IntPtr hIcon);
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(ref NetDriveManager.NETRESOURCE netResource, string password, string Username, int Flag);
[DllImport("mpr.dll")]
private static extern int WNetAddConnection3(IntPtr hwnd, ref NetDriveManager.NETRESOURCE netResource, string password, string Username, int Flag);
[DllImport("mpr.dll")]
private static extern int WNetGetConnection(string localName, StringBuilder remoteName, ref int length);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string lpName, int dwFlags, bool fForce);
public static void MountDrive(char letter, string unc, string name, string iconpath, int index)
{
var v0 = new NetDriveManager.NETRESOURCE()
{
dwType = NetDriveManager.ResourceType.Disk,
lpLocalName = Conversions.ToString(letter) + ":",
lpRemoteName = unc,
lpProvider = (string) null
};
int num = NetDriveManager.WNetAddConnection2(ref v0, "", "", 0);
if (num != 0)
throw new Exception(num.ToString());
Registry.SetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\MountPoints2\\" + unc.Replace("\\", "#"), "_LabelFromReg", (object) name);
NetDriveManager.SHFILEINFO psfi = new NetDriveManager.SHFILEINFO();
IntPtr fileInfo = NetDriveManager.SHGetFileInfo(Conversions.ToString(letter) + ":", 0U, ref psfi, Marshal.SizeOf((object) psfi), 4096U);
string str = "";
int iIcon1=0;
int iIcon2=0;
if (fileInfo != IntPtr.Zero)
{
str = psfi.szDisplayName;
iIcon1 = psfi.iIcon;
if (NetDriveManager.SHGetFileInfo(Conversions.ToString(letter) + ":", 0U, ref psfi, Marshal.SizeOf((object) psfi), 256U) != IntPtr.Zero)
{
iIcon2 = psfi.iIcon;
NetDriveManager.DestroyIcon(psfi.hIcon);
}
}
Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\Applications\\Explorer.exe\\Drives\\" + Conversions.ToString(letter) + "\\DefaultIcon", (string) null, (object) (iconpath + "," + index.ToString()));
if (Operators.CompareString(str, "", false) == 0)
return;
NetDriveManager.SHUpdateImage(str, iIcon1, 0, iIcon2);
}
public static void UnmountDrive(char letter)
{
string str = "";
int iIcon1=0;
int iIcon2=0;
try
{
NetDriveManager.SHFILEINFO psfi = new NetDriveManager.SHFILEINFO();
if (NetDriveManager.SHGetFileInfo(Conversions.ToString(letter) + ":", 0U, ref psfi, Marshal.SizeOf((object) psfi), 4096U) != IntPtr.Zero)
{
str = psfi.szDisplayName;
iIcon1 = psfi.iIcon;
if (NetDriveManager.SHGetFileInfo(Conversions.ToString(letter) + ":", 0U, ref psfi, Marshal.SizeOf((object) psfi), 256U) != IntPtr.Zero)
{
iIcon2 = psfi.iIcon;
NetDriveManager.DestroyIcon(psfi.hIcon);
}
}
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
ProjectData.ClearProjectError();
}
NetDriveManager.WNetCancelConnection2(Conversions.ToString(letter) + ":", 0, true);
try
{
Registry.CurrentUser.DeleteSubKeyTree("Software\\Classes\\Applications\\Explorer.exe\\Drives\\" + Conversions.ToString(letter));
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
ProjectData.ClearProjectError();
}
try
{
if (Operators.CompareString(str, "", false) == 0)
return;
NetDriveManager.SHUpdateImage(str, iIcon1, 0, iIcon2);
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
ProjectData.ClearProjectError();
}
}
public static void MapFolder(string unc, string name, string iconpath, int iconindex)
{
try
{
object objectValue1 = RuntimeHelpers.GetObjectValue(Interaction.CreateObject("WScript.Shell", ""));
System.Type typeFromProgId = System.Type.GetTypeFromProgID("Shell.Application");
object objectValue2 = RuntimeHelpers.GetObjectValue(Activator.CreateInstance(typeFromProgId));
object objectValue3 = RuntimeHelpers.GetObjectValue(typeFromProgId.InvokeMember("Namespace", BindingFlags.InvokeMethod, (Binder) null, RuntimeHelpers.GetObjectValue(objectValue2), new object[1]
{
(object) 19
}));
object objectValue4 = RuntimeHelpers.GetObjectValue(objectValue3.GetType().InvokeMember("Self", BindingFlags.GetProperty, (Binder) null, RuntimeHelpers.GetObjectValue(objectValue3), (object[]) null));
string str = objectValue4.GetType().InvokeMember("Path", BindingFlags.GetProperty, (Binder) null, RuntimeHelpers.GetObjectValue(objectValue4), new object[0]).ToString();
DirectoryInfo directoryInfo = new DirectoryInfo(str + "\\" + name);
if (directoryInfo.Exists)
{
directoryInfo.Attributes = FileAttributes.Directory;
directoryInfo.Delete(true);
}
directoryInfo.Create();
object objectValue5 = RuntimeHelpers.GetObjectValue(objectValue1.GetType().InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, (Binder) null, RuntimeHelpers.GetObjectValue(objectValue1), new object[1]
{
(object) (str + "\\" + name + "\\target.lnk")
}));
objectValue5.GetType().InvokeMember("TargetPath", BindingFlags.SetProperty, (Binder) null, RuntimeHelpers.GetObjectValue(objectValue5), new object[1]
{
(object) unc
});
objectValue5.GetType().InvokeMember("IconLocation", BindingFlags.SetProperty, (Binder) null, RuntimeHelpers.GetObjectValue(objectValue5), new object[1]
{
(object) (iconpath + "," + iconindex.ToString())
});
objectValue5.GetType().InvokeMember("Save", BindingFlags.InvokeMethod, (Binder) null, RuntimeHelpers.GetObjectValue(objectValue5), new object[0]);
FileInfo fileInfo = new FileInfo(str + "\\" + name + "\\desktop.ini");
using (StreamWriter streamWriter = new StreamWriter((Stream) fileInfo.Create()))
{
streamWriter.WriteLine("[.ShellClassInfo]");
streamWriter.WriteLine("CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}");
streamWriter.WriteLine("Flags=2");
streamWriter.WriteLine("IconFile=" + iconpath);
streamWriter.WriteLine("IconIndex=" + Conversions.ToString(iconindex));
streamWriter.WriteLine("IconResource=" + iconpath + "," + Conversions.ToString(iconindex));
}
fileInfo.Attributes = FileAttributes.Hidden | FileAttributes.System;
directoryInfo.Attributes = FileAttributes.ReadOnly | FileAttributes.NotContentIndexed;
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
int num = (int) MessageBox.Show(ex.ToString());
ProjectData.ClearProjectError();
}
}
public static void UnmapFolder(string name)
{
try
{
RuntimeHelpers.GetObjectValue(Interaction.CreateObject("WScript.Shell", ""));
System.Type typeFromProgId = System.Type.GetTypeFromProgID("Shell.Application");
object objectValue1 = RuntimeHelpers.GetObjectValue(Activator.CreateInstance(typeFromProgId));
object objectValue2 = RuntimeHelpers.GetObjectValue(typeFromProgId.InvokeMember("Namespace", BindingFlags.InvokeMethod, (Binder) null, RuntimeHelpers.GetObjectValue(objectValue1), new object[1]
{
(object) 19
}));
object objectValue3 = RuntimeHelpers.GetObjectValue(objectValue2.GetType().InvokeMember("Self", BindingFlags.GetProperty, (Binder) null, RuntimeHelpers.GetObjectValue(objectValue2), (object[]) null));
DirectoryInfo directoryInfo = new DirectoryInfo(objectValue3.GetType().InvokeMember("Path", BindingFlags.GetProperty, (Binder) null, RuntimeHelpers.GetObjectValue(objectValue3), new object[0]).ToString() + "\\" + name);
if (!directoryInfo.Exists)
return;
directoryInfo.Attributes = FileAttributes.Directory;
directoryInfo.Delete(true);
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
ProjectData.ClearProjectError();
}
}
public static char FindMountedDriveLetter(string unc)
{
DriveInfo[] drives = DriveInfo.GetDrives();
int index = 0;
while (index < drives.Length)
{
DriveInfo driveInfo = drives[index];
if (driveInfo.DriveType == DriveType.Network)
{
StringBuilder stringBuilder1 = new StringBuilder(4096);
string localName = Conversions.ToString(driveInfo.Name[0]) + ":";
StringBuilder remoteName = stringBuilder1;
StringBuilder stringBuilder2 = stringBuilder1;
int capacity = stringBuilder2.Capacity;
int connection = NetDriveManager.WNetGetConnection(localName, remoteName, ref capacity);
stringBuilder2.Capacity = capacity;
if (connection == 0 && Operators.CompareString(stringBuilder1.ToString(), unc, false) == 0)
return driveInfo.Name[0];
}
checked { ++index; }
}
return char.MinValue;
}
public static long ReadMaxFileSize()
{
int integer = Conversions.ToInteger(Registry.GetValue("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\WebClient\\Parameters", "FileSizeLimitInBytes", (object) 50000000));
if (integer < 0)
return checked (4294967296L + (long) integer);
return (long) integer;
}
public static void WriteMaxFileSize(long size, IntPtr hwnd)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.FileName = "reg.exe";
startInfo.Verb = "runas";
startInfo.Arguments = "add HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\WebClient\\Parameters /v FileSizeLimitInBytes /t REG_DWORD /d " + size.ToString() + " /f";
startInfo.ErrorDialog = true;
startInfo.ErrorDialogParentHandle = hwnd;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
try
{
Process.Start(startInfo).WaitForExit();
}
catch (Win32Exception ex)
{
ProjectData.SetProjectError((Exception) ex);
ProjectData.ClearProjectError();
}
}
public static void RebootWebClient(IntPtr hwnd)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.FileName = Environment.GetEnvironmentVariable("ComSpec");
startInfo.Verb = "runas";
startInfo.ErrorDialog = true;
startInfo.ErrorDialogParentHandle = hwnd;
startInfo.CreateNoWindow = true;
startInfo.Arguments = "/c net stop webclient & net stop mrxdav & net start mrxdav & net start webclient & pause";
try
{
Process.Start(startInfo).WaitForExit();
}
catch (Win32Exception ex)
{
ProjectData.SetProjectError((Exception) ex);
ProjectData.ClearProjectError();
}
}
private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
public struct NETRESOURCE
{
public NetDriveManager.ResourceScope dwScope;
public NetDriveManager.ResourceType dwType;
public NetDriveManager.ResourceDisplayType dwDisplayType;
public NetDriveManager.ResourceUsage dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;
}
public enum ResourceScope : uint
{
Connected = 1,
GlobalNet = 2,
Remembered = 3,
Recent = 4,
Context = 5,
}
public enum ResourceType : uint
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
Unknown = 4294967295,
}
public enum ResourceDisplayType : uint
{
Generic,
Domaim,
Server,
Share,
File,
Group,
Network,
Root,
ShareAdmin,
Directory,
Tree,
NDSContainer,
}
public enum ResourceUsage : uint
{
Connectable = 1,
Container = 2,
NoLocalDevice = 4,
Sibling = 8,
Attached = 16,
All = 19,
Reserved = 2147483648,
}
}
}