我注意到,該方法Environment.ExpandEnvironmentVariables()
不會返回某些系統變量的值,如%date%
,%time%
,%homeshare%
等...爲什麼?Environment.ExpandEnvironmentVariables不適用於所有變量
3
A
回答
4
%HOMESHARE%
可能僅僅是未定義的(它並未在所有情況下定義)。 %DATE%
和%TIME%
是AFAIK在CMD
之外不可用的動態變量(對於例如%CD%
和%ERRORLEVEL%
也是如此)。
2
@Ansgar Wiechers當然是正確的,我認爲我將代碼提供給以下函數,該函數也試圖替換(某些)CMD.EXE
特定變量。
public static string ExpandEnvironmentVariables(string str)
{
// Environment.ExpandEnvironmentVariables() does this as well, but we don't rely on that.
if (str == null)
throw new ArgumentNullException("str");
// First let .NET Fx version do its thing, because then:
//
// - Permission checks, etc. will already be done up front.
// - Should %CD% already exists as a user defined variable, it will already be replaced,
// and we don't do it by the CurrentDirectory later on. This behavior is consistent with
// what CMD.EXE does.
// Also see http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx.
//
str = Environment.ExpandEnvironmentVariables(str);
// The following is rather expensive, so a quick check if anything *could* be required at all
// seems to be warrented.
if (str.IndexOf('%') != -1)
{
const StringComparison comp = StringComparison.OrdinalIgnoreCase;
var invariantCulture = CultureInfo.InvariantCulture;
var now = DateTime.Now;
str = str.Replace("%CD%", Environment.CurrentDirectory, comp);
str = str.Replace("%TIME%", now.ToString("T") + "," + now.ToString("ff"), comp);
str = str.Replace("%DATE%", now.ToString("d"), comp);
str = str.Replace("%RANDOM%", s_random.Next(0, Int16.MaxValue).ToString(invariantCulture), comp);
// Debatable, but we replace them anyway to make sure callers don't "crash" because
// them not being unexpanded, and becase we "can".
str = str.Replace("%CMDEXTVERSION%", "2", comp); // This is true at least on XP to Server 2008R2
str = str.Replace("%CMDCMDLINE%", Environment.CommandLine, comp);
uint nodeNumber;
if (!NativeMethods.GetNumaHighestNodeNumber(out nodeNumber))
{
nodeNumber = 0;
}
str = str.Replace("%HIGHESTNUMANODENUMBER%", nodeNumber.ToString(invariantCulture), comp);
}
return str;
}
爲GetNumaHighestNodeNumber
P中的定義/調用如下:
[DllImport(KernelDll)]
[return: MarshalAsAttribute(UnmanagedType.Bool)]
public static extern bool GetNumaHighestNodeNumber([Out] out uint HighestNodeNumber);
相關問題
- 1. __attribute__是否適用於聲明中的所有變量?
- 2. Cakephp 3 - 適用於所有型號的變量
- 3. php SELECT查詢適用於值,但不適用於變量
- 4. 使用esttab時,爲什麼keep()子選項不適用於所有變量?
- 5. CGContextSetFillColorWithColor不適用於顏色變量
- 6. VaryByCustom不適用於會話變量
- 7. Oracle綁定變量不適用於ActiveRecord
- 8. css方法不適用於變量
- 9. $ _POST不適用於url變量
- 10. 變量不適用於動畫滾動
- 11. 減法不適用於一個變量
- 12. clearTimeout不適用於popover變量undefined
- 13. oracle替換變量不適用於varchar2
- 14. DOM appendChild不適用於兩個變量
- 15. WHERE子句不適用於PHP變量
- 16. ng-maxlength不適用於變量
- 17. jQuery .size()函數不適用於變量
- 18. 芹菜不適用於全局變量
- 19. AngularJS - 手錶不適用於變量
- 20. PDO適用於所有但不適用於Cron Yii2
- 21. NSImage不適用於所有圖像
- 22. Vimperator不適用於所有模式
- 23. jQuery/JavaScript不適用於所有頁面
- 24. 'toggleClass'不適用於所有其他div
- 25. ActionListener不適用於所有的對象
- 26. soa.delete_instances_in_parallel不適用於所有分區
- 27. SharedPreferences不適用於所有活動
- 28. Request.form不適用於所有字段
- 29. Jersey ContainerResponseFilter不適用於所有響應
- 30. UILongPressGestureRecognizer不適用於所有UIButtons
+1:雖然我們在這,同樣也適用於'%RANDOM%','%HIGHESTNUMANODENUMBER% ','%CMDEXTVERSION%%'和'%CMDCMDLINE%' - 雖然後面的看起來很明顯;-) –