如何从 CMD 更改网络驱动器的名称?

问题描述 投票:0回答:1

我正在创建一个测试程序,该程序创建一个带有字母“S:”的卷(这是一个网络文件夹)。

为此,我正在使用:

net use {driveLetter}: "{remotePath}"

问题是,当我尝试更改名称时,收到错误“参数不兼容”

这是我使用的代码:

// Now, set the custom name for the network drive
Process setNameProcess = new Process();
ProcessStartInfo setNameStartInfo = new ProcessStartInfo
{
    FileName = "cmd.exe",
    Arguments = $"/C label {driveLetter}: \"{volumeName}\"",
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    UseShellExecute = false,
    CreateNoWindow = true
};
setNameProcess.StartInfo = setNameStartInfo;
setNameProcess.Start();
setNameProcess.WaitForExit();

string output = setNameProcess.StandardOutput.ReadToEnd();
MessageBox.Show("output" + output);
string error = setNameProcess.StandardError.ReadToEnd();
MessageBox.Show("error " + error);

// Check if the name setting operation was successful
if (setNameProcess.ExitCode == 0)
{
    MessageBox.Show($"El nombre '{volumeName}' se estableció correctamente para la unidad de red '{driveLetter}:'.");
}
else
{
    MessageBox.Show($"Error al establecer el nombre '{volumeName}' para la unidad de red '{driveLetter}:'");
}
c# windows cmd label volumes
1个回答
0
投票

我通过使用基于 Guy Thomas 脚本

的 .vbs 脚本找到了解决方案

这是我的代码:

C#

// I create the network Drive
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
    FileName = "cmd.exe",
    Arguments = $"/C {command}",
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true
};

process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

// If the creation was successful
if (process.ExitCode == 0)
{
    ProcessStartInfo cambioNombre = new ProcessStartInfo(); 
    cambioNombre.FileName = "wscript.exe"; // I launch the script
    cambioNombre.Arguments = $"\"{scriptPath}\" \"{volumeName}\"";
    cambioNombre.UseShellExecute = false;
    cambioNombre.RedirectStandardOutput = true;

    Process nombre = new Process(); 
    nombre.StartInfo = cambioNombre; 
    nombre.Start(); 

    process.WaitForExit();

    string output = process.StandardOutput.ReadToEnd();
    Console.WriteLine(output);

    MessageBox.Show($"La unidad de red '{driveLetter}:' se creó correctamente.");
}
else
{
    MessageBox.Show($"Error al crear la unidad de red '{driveLetter}:'");
}

脚本

Option Explicit

Dim strDriveLetter, strNewName
strDriveLetter = "S:"
strNewName = WScript.Arguments(0)

' Rename network drive
Dim objShell
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter).Self.Name = strNewName

' Show confirmation message
WScript.Echo "Se ha cambiado el nombre de la unidad " & strDriveLetter & " a " & strNewName

我不知道这是否是最有效和最优化的解决方案,但目前它确实有效,我附上了最终结果的图像,它是具有个性化名称的网络驱动器。

© www.soinside.com 2019 - 2024. All rights reserved.