2016-06-14 51 views
1
get 
{ 
    string dirName = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
    string pathName; 
    DirectoryInfo d = new DirectoryInfo("TradeBotData"); 
    if (!d.Exists) 
    { 
     if (d.Parent.Name.ToString() == "Plugins") 
     { 
      d.Create(); 
      return d.FullName; 
     } 
    } 
    else 
    { 
     if (d.Parent.Name.ToString() == "Plugins") 
     { 
      return d.FullName; 
     } 
     else 
     { 
      Console.WriteLine("Data path Fallback!!!"); 
      pathName = System.IO.Path.Combine(dirName, @"\TradeBotData"); 
      System.IO.Directory.CreateDirectory(pathName); 
      Console.WriteLine("Created Save Folder At: {0} :", pathName); 
      return pathName; 
     } 
    } 
} 

我不太確定爲什麼會發生這種情況。我認爲所有代碼路徑都會返回一個值,因爲我有ifelse。 如果我在代碼中插入return "";,它只是返回「」;無限地。c#編譯器返回錯誤不是所有代碼路徑都返回一個值

請指教。

+3

你必須返回的東西的時候'如果(d.Parent.Name.ToString()==「插件」)'失敗。 –

+3

要擴大哈里的評論:在目錄不存在的情況下,所以你要進入第一個「if」陳述的主體;如果父母名稱不是「插件」,則不會返回任何內容。 –

回答

3

如果您DirectoryInfo不存在,編譯器會檢查是否d.Parent.Name.ToString() == "Plugins"如果是,withing的if語句的代碼會被執行否則編譯器會拋出異常:「並非所有的代碼路徑返回一個值」因爲你不會返回任何東西

所以你缺少一個return這裏:

get 
{ 
string dirName =System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
    string pathName; 
    DirectoryInfo d = new DirectoryInfo("TradeBotData"); 
    if (!d.Exists) 
    { 
     if (d.Parent.Name.ToString() == "Plugins") 
     { 
      d.Create(); 
      return d.FullName; 
     } 
    //////////HERE/////// 
    } 
    else 
    { 
     if (d.Parent.Name.ToString() == "Plugins") 
     { 
      return d.FullName; 
     } 
     else 
     { 
      Console.WriteLine("Data path Fallback!!!"); 
      pathName = System.IO.Path.Combine(dirName, @"\TradeBotData"); 
      System.IO.Directory.CreateDirectory(pathName); 
      Console.WriteLine("Created Save Folder At: {0} :", pathName); 
      return pathName; 
     } 
    } 
} 
+0

想要做什麼,解決這個問題? –

+0

非幸運我不確定你的意思。 – user3378165

+0

你已經確定了這個問題,爲了讓它成爲你的完美答案,你需要爲他建議解決方案 –

0

初始化路徑名空字符串。然後將其設置在你的if/else邏輯,並在和get只返回:

get 
      { 
       string dirName = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
       string pathName = String.Empty; 
       DirectoryInfo d = new DirectoryInfo("TradeBotData"); 
       if (!d.Exists) { 
       if (d.Parent.Name == "Plugins") { 
        d.Create(); 
        pathName = d.FullName; 
       } 
       } else { 
       if (d.Parent.Name == "Plugins") { 
        pathName = d.FullName; 
       } else { 
        Console.WriteLine("Data path Fallback!!!"); 
        pathName = System.IO.Path.Combine(dirName, @"\TradeBotData"); 
        System.IO.Directory.CreateDirectory(pathName); 
        Console.WriteLine("Created Save Folder At: {0} :", pathName); 

       } 
       } 

       return pathName; 
      } 
相關問題