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;
        }
    }
}

I'm not really sure why this happens. I think that all code paths return a value because I have if and else. If I insert return ""; into the code it just returns ""; infinitely.

Please advise.

user3378165
  • 6,546
  • 17
  • 62
  • 101
James Meas
  • 327
  • 4
  • 16
  • 3
    you have to return something when `if (d.Parent.Name.ToString() == "Plugins")` fails. – Hari Prasad Jun 14 '16 at 08:13
  • 3
    To expand Hari's comment: In the case where the directory doesn't exist, so you're going into the body of the first `if` statment; if the parent name isn't `Plugins`, you then don't return anything. – Jon Skeet Jun 14 '16 at 08:15

2 Answers2

3

If your DirectoryInfo doesn't exist, the compiler will check if d.Parent.Name.ToString() == "Plugins" if it is, the code withing the if statement will be executed but otherwise the compiler will throw an exception: "not all code path return a value" because you don't return anything.

So you are missing a return here:

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;
        }
    }
}
user3378165
  • 6,546
  • 17
  • 62
  • 101
0

Initialize pathName to empty string. Then set it in your if/else logic and in the and of get just return it:

  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;
            }
Radin Gospodinov
  • 2,313
  • 13
  • 14