Notifications
Clear all
[Closed] Remove drive letter with C# in maxscript
Oct 13, 2016 11:55 am
Tried to do myself but can’t get to the solution. Please help.
(
global DriveAssembly
fn CreateDriveAssembly forceRecompile:on =
(
if forceRecompile or not iskindof ::DriveAssembly dotnetobject or (::DriveAssembly.GetType()).name != "Assembly" do
(
source = ""
source += "using System;
"
source += "using System.Runtime.InteropServices;
"
source += "public class DriveOps
"
source += "{
"
source += " [DllImport(\"kernel32.dll\", CharSet = CharSet.Auto, SetLastError = true)]
"
source += " internal static extern bool DeleteVolumeMountPoint(
"
source += " in string directoryName;
"
source += " );
"
source += " public static bool RemDrive(string directoryName)
"
source += " {
"
source += " DeleteVolumeMountPoint(directoryName); "
source += " return true;
"
source += " }
"
source += "}
"
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.ReferencedAssemblies.Add("System.dll");
compilerParams.GenerateInMemory = true
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
DriveAssembly = compilerResults.CompiledAssembly
)
)
-------------------------------------------------------------------------
CreateDriveAssembly forceRecompile:on
global RemoveDriveLetter = (DriveAssembly.CreateInstance "DriveOps").RemDrive
RemoveDriveLetter "G:\\"
)
4 Replies
Oct 13, 2016 11:55 am
What is your goal here? Messing with mount points sounds a tad bit invasive for a max plugin to do on a user’s system, not to mention it likely requires administrator rigths. If it’s a temporary drive letter you need, have you considered using virtual drives instead? If things go wrong, at least they won’t persist through a reboot.
Removing a virtual drive would be very easy, too:
fn RemoveDriveLetter driveLetter = ( hiddenDOSCommand ("subst " + driveLetter[1] + ": /D") )
Oct 13, 2016 11:55 am
In fact Iwant to use this for my personal computer only, so nobody will suffer I do it manually, but wanted to automate.
Oct 13, 2016 11:55 am
global DriveAssembly
fn CreateDriveAssembly =
(
source = ""
source += "using System;
"
source += "using System.Runtime.InteropServices;
"
source += "public class DriveOps
"
source += "{
"
source += " [DllImport(\"kernel32.dll\", CharSet = CharSet.Auto, SetLastError = true)]
"
source += " public static extern bool DeleteVolumeMountPoint(String directoryName);
"
source += "}
"
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.ReferencedAssemblies.Add "System.dll"
compilerParams.GenerateInMemory = true
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
DriveAssembly = compilerResults.CompiledAssembly
)
-------------------------------------------------------------------------
CreateDriveAssembly()
(
DriveOps = DriveAssembly.CreateInstance "DriveOps"
DriveOps.DeleteVolumeMountPoint "G:\\"
)
i’ve just fixed c# syntax and couple bugs
it’s not a good habit to rename library function if don’t change its sense, input or output parameters