![](/img/trans.png)
[英]How to get Visual Studio 2013 to not stop the build on errors (C++)
[英]How to get current directory of a specific drive in Visual Studio Express 2013(C++)?
我正在將程序從Borland C ++ Builder移植到Visual Studio 2013(C ++)。 該程序使用getcurdir獲取驅動器的當前目錄。 此函數具有參數驅動器,但是Microsoft等效函數getcwd沒有此類參數。 我該怎么做?
在標記Visual Studio時,我假設您正在使用Windows。 我認為,當前目錄旁邊只有一個目錄(即可執行文件所在的位置,或者如果您移至其他目錄,則當前目錄也不會有所不同),具體取決於當前驅動器。 然后,在Windows中,您可以使用winapi中的功能GetCurrentDirectory
。 原型是:
DWORD WINAPI GetCurrentDirectory(
_In_ DWORD nBufferLength,
_Out_ LPTSTR lpBuffer
);
您可以在此處獲取詳細信息。
例:
TCHAR cwd[100];
GetCurrentDirectory(100,cwd);
// now cwd will contain absolute path to current directory
(是的,我知道這是一個舊條目,僅供記錄,如果有人偶然發現同一問題...)
. 正如deeiip正確說的那樣,在Windows中只有1個當前目錄,但是當有1個時,cmd.exe會偽造DOS行為。
, use the according hidden environment variables, eg "%=C:%". 如果需要訪問 cmd ,請使用相應的隱藏環境變量,例如“%= C:%”。
這是一個示例應用程序(在C#中):
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.