Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Visual Studio Express 2013(C+;+;)中获取特定驱动器的当前目录? 我正在从Borland C++ Builder移植到Visual Studio 2013(C++)。程序使用getcurdir获取驱动器的当前目录。此函数具有参数驱动器,但Microsoft等效函数getcwd没有此类参数。我该怎么做呢?_C++_Visual Studio 2010_Visual Studio_Visual Studio 2012 - Fatal编程技术网

如何在Visual Studio Express 2013(C+;+;)中获取特定驱动器的当前目录? 我正在从Borland C++ Builder移植到Visual Studio 2013(C++)。程序使用getcurdir获取驱动器的当前目录。此函数具有参数驱动器,但Microsoft等效函数getcwd没有此类参数。我该怎么做呢?

如何在Visual Studio Express 2013(C+;+;)中获取特定驱动器的当前目录? 我正在从Borland C++ Builder移植到Visual Studio 2013(C++)。程序使用getcurdir获取驱动器的当前目录。此函数具有参数驱动器,但Microsoft等效函数getcwd没有此类参数。我该怎么做呢?,c++,visual-studio-2010,visual-studio,visual-studio-2012,C++,Visual Studio 2010,Visual Studio,Visual Studio 2012,当您标记visual studio时,我假设您正在使用windows。除此之外,当前目录只是一个(即可执行文件所在的位置或其他位置,如果您已移动到),我认为当前目录不会因当前驱动器的不同而有所不同。然后,在windows中,您可以使用winapi中的函数GetCurrentDirectory。原型是: DWORD WINAPI GetCurrentDirectory( _In_ DWORD nBufferLength, _Out_ LPTSTR lpBuffer ); 您可以获得

当您标记visual studio时,我假设您正在使用windows。除此之外,当前目录只是一个(即可执行文件所在的位置或其他位置,如果您已移动到),我认为当前目录不会因当前驱动器的不同而有所不同。然后,在windows中,您可以使用winapi中的函数
GetCurrentDirectory
。原型是:

DWORD WINAPI GetCurrentDirectory(
  _In_   DWORD nBufferLength,
  _Out_  LPTSTR lpBuffer
);
您可以获得详细信息。 例如:

(是的,我知道这是一个旧条目,如果有人在同一个问题上绊倒,只是为了记录在案…)

如前所述,在Windows中只有一个当前目录,但当每个驱动器有一个当前目录时,cmd.exe会伪造DOS行为

如果每个驱动器需要访问cmd的当前目录,请使用相应的隐藏环境变量,例如“%=C:%”

下面是一个示例应用程序(C#):

使用系统;
静态类模块1{
公共静态void Main(字符串[]args){
var myFolder=GetCurrentFolderPerDrive(args[0]);//例如“C:”
Console.WriteLine(myFolder);
}
私有静态字符串GetCurrentFolderPerDrive(字符串driveLetter){
driveLetter=规范化的driveLetter(driveLetter);
字符串myEnvironmentVariable=$“%={driveLetter}%”;
字符串myResult=Environment.ExpandEnvironmentVariables(myEnvironmentVariable);
如果(myResult==myEnvironmentVariable)返回$“{driveLetter.ToUpperInvariant()}\\”;//未设置当前文件夹,则返回root
返回我的结果;
}
私有静态字符串normalizedDriveLetter(字符串driveLetter){
如果(String.IsNullOrWhiteSpace(driveLetter))抛出新的ArgumentNullException(nameof(driveLetter)),“驱动器号为null、空或空白。”;
布尔ThroweException=((“ABCDEFGHIjklmnopqrStuvxyzabCDEFGHIjklmnopqrStuvxyz”).IndexOf(driveLetter[0])<0;
如果(!ThroweException){
如果(driveLetter.Length==1){
driveLetter+=':';
}else if(driveLetter.Length!=2){
ThroweException=true;
}
}
如果(ThroweException)抛出新的ArgumentException($),则需要一个格式良好的驱动器号,例如\“C:\”!\r\n文件值:\“{drivelette}\”,名称(drivelette));
回信;
}
}
TCHAR cwd[100];
GetCurrentDirectory(100,cwd);
// now cwd will contain absolute path to current directory
using System;

static class Module1 {

    public static void Main(String[] args) {
        var myFolder = GetCurrentFolderPerDrive(args[0]); //e.g. "C:"
        Console.WriteLine(myFolder);
    }

    private static string GetCurrentFolderPerDrive(string driveLetter) {
        driveLetter = NormalizeDriveLetter(driveLetter);
        string myEnvironmentVariable = $"%={driveLetter}%";
        string myResult = Environment.ExpandEnvironmentVariables(myEnvironmentVariable);
        if (myResult == myEnvironmentVariable) return $"{driveLetter.ToUpperInvariant()}\\"; //No current folder set, return root
        return myResult;
    }

    private static String NormalizeDriveLetter(String driveLetter) {
        if (String.IsNullOrWhiteSpace(driveLetter)) throw new ArgumentNullException(nameof(driveLetter), "The drive letter is null, empty or white-space.");
        Boolean throwException = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".IndexOf(driveLetter[0]) < 0);
        if (!throwException) {
            if (driveLetter.Length == 1) {
                driveLetter += ':';
            } else if (driveLetter.Length != 2) {
                throwException = true;
            }
        }
        if (throwException) throw new ArgumentException($"A well-formed drive letter expected, e.g. \"C:\"!\r\nGiven value: \"{driveLetter}\".", nameof(driveLetter));
        return driveLetter;
    }

}