Je veux sortir D: \ Apprentissage \ CS \ Resource \ Tutoriel \ C# LangTutorial Mais ne peut pas fonctionner. Erreur de compilation erreur CS0165: Utilisation de la variable locale non affectée 'StrPathHead S'il vous plaît donnez-moi quelques conseils sur la façon de corriger mon code ou une autre meilleure solution pour mon cas. Je vous remercie.Chemin Chaîne Concaténation Question en C#
static void Main(string[] args)
{
string path = "D:\\Learning\\CS\\Resource\\Book\\C#InDepth";
int n = 0;
string[] words = path.Split('\\');
foreach (string word in words)
{
string StrPathHead;
string StrPath;
Console.WriteLine(word);
if (word == "Resource")
{
StrPath = StrPathHead + word + "\\Tutorial\\C#LangTutorial";
}
else
{
StrPathHead += words[n++] + "\\";
}
}
}
utilisation Path.Combine() .... –
vous n'avez pas initialisées strPath; Vous avez également déclaré dans le périmètre de la boucle interne donc sera remplacé à chaque fois par la boucle, donc ne fonctionnera pas actuellement comme vous l'avez l'intention. –