c#编译器返回错误并非所有代码路径都返回值
我不太清楚为什么会这样。我认为所有代码路径都返回一个值,因为我有c#编译器返回错误并非所有代码路径都返回值,c#,C#,我不太清楚为什么会这样。我认为所有代码路径都返回一个值,因为我有if和else。 如果我插入返回“”转换为只返回“”的代码;无限 请告知。如果您的DirectoryInfo不存在,编译器将检查d.Parent.Name.ToString()==“Plugins”如果存在,将执行带有If语句的代码,否则编译器将抛出异常:“并非所有代码路径都返回值”因为您不返回任何内容 因此,您缺少一个返回值: get { string dirName = System.IO.Path.GetDirecto
if
和else
。
如果我插入返回“”
转换为只返回“”的代码;无限
请告知。如果您的
DirectoryInfo
不存在,编译器将检查d.Parent.Name.ToString()==“Plugins”
如果存在,将执行带有If
语句的代码,否则编译器将抛出异常:“并非所有代码路径都返回值”因为您不返回任何内容
因此,您缺少一个返回值
:
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;
}
}
}
将路径名初始化为空字符串。然后在if/else逻辑中设置它,并在
get
的and中返回它:
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;
}
}
}
如果(
if(d.Parent.Name.ToString()=“Plugins”)
失败,您必须返回一些内容。要展开Hari的注释:如果目录不存在,那么您将进入第一个if
语句的主体;如果父名称不是插件
,则不返回任何内容。op想要做什么,来解决此问题?运气不好,我不确定您的意思。您已经确定了此问题,为了使其成为一个完美的答案,您需要建议解决方案him@user3378165在提供解决方案的同时,解释问题所在以及如何解决问题(一些细节)。谢谢你的评论,你完全正确,我将编辑我的答案,包括解释。谢谢!
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;
}