![](/img/trans.png)
[英]How do I Check and compare if a string is matched in a enum and return its position integer in a new variable C#
[英]C# how do i return an environment variable as a string
在C#中,我想獲取環境變量的列表,並將其轉換為字符串。 但是,當我執行代碼時,出現錯誤:
嵌入式語句不能是聲明或帶標簽的語句
在字符串teststring = de.Value;
using System;
using System.Collections;
using System.IO;
class Sample
{
public static void Main()
{
Console.WriteLine();
Console.WriteLine("GetEnvironmentVariables: ");
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
string teststring = de.Value;
string testpath = String.Format("\nValue as string: ",teststring);
Console.WriteLine(testpath);
Console.WriteLine("\n {0} = {1}", de.Key, de.Value);
}
}
如何解決此問題,為什么會發生此錯誤?
您需要將{
和}
放在foreach
塊周圍,因為:
public static void Main()
{
Console.WriteLine();
Console.WriteLine("GetEnvironmentVariables: ");
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
{
string teststring = de.Value;
string testpath = String.Format("\nValue as string: ",teststring);
Console.WriteLine(testpath);
Console.WriteLine("\n {0} = {1}", de.Key, de.Value);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.