There’s one challenge we rarely think about when writing file-handling code: how do we know where the file actually is? Josh H. inherited some C# code that puts a lot of thought into that. A lot.

        longFileName = "x:\\isdn\\" + fileName;
        if (File.Exists(longFileName))
            fileExistsFlag = true;
        else if (!File.Exists(longFileName))
        {
            longFileName = "Y:\\isdnX\\" + fileName;
            if (File.Exists(longFileName))
                fileExistsFlag = true;
            else
            {
                longFileName = "z:\\isdn\\" + fileName;
                if (File.Exists(longFileName))
                    fileExistsFlag = true;
                else
                {
                    longFileName = "g:\\isdn\\a\\" + fileName;
                    if (File.Exists(longFileName))
                        fileExistsFlag = true;
                    else
                    {
                        longFileName = "g:\\isdn\\b\\" + fileName;
                        if (File.Exists(longFileName))
                            fileExistsFlag = true;
                        else
                        {
                            longFileName = "g:\\isdn\\c\\" + fileName;
                            if (File.Exists(longFileName))
                                fileExistsFlag = true;
                            else
                            {
                                longFileName = "g:\\isdn\\mbna\\" + fileName;
                                if (File.Exists(longFileName))
                                    fileExistsFlag = true;
                                else
                                {
                                    longFileName = "g:\\isdn\\sales\\" + fileName;
                                    if (File.Exists(longFileName))
                                        fileExistsFlag = true;
                                    else
                                    {
                                        longFileName = "g:\\isdn\\w\\" + fileName;
                                        if (File.Exists(longFileName))
                                            fileExistsFlag = true;
                                        else
                                        {
                                            longFileName = "g:\\isdn\\x\\" + fileName;
                                            if (File.Exists(longFileName))
                                                fileExistsFlag = true;
                                            else
                                            {
                                                longFileName = "g:\\isdn\\y\\" + fileName;
                                                if (File.Exists(longFileName))
                                                    fileExistsFlag = true;
                                                else
                                                {
                                                    longFileName = "g:\\isdn\\z\\" + fileName;
                                                    if (File.Exists(longFileName))
                                                        fileExistsFlag = true;
                                                    else
                                                    {
                                                        longFileName = "h:\\isdn\\a\\" + fileName;
                                                        if (File.Exists(longFileName))
                                                            fileExistsFlag = true;
                                                        else
                                                        {
                                                            longFileName = "h:\\isdn\\b\\" + fileName;
                                                            if (File.Exists(longFileName))
                                                                fileExistsFlag = true;
                                                            else
                                                            {
                                                                longFileName = "h:\\isdn\\c\\" + fileName;
                                                                if (File.Exists(longFileName))
                                                                    fileExistsFlag = true;
                                                                else
                                                                {
                                                                    longFileName = "h:\\isdn\\mbna\\" + fileName;
                                                                    if (File.Exists(longFileName))
                                                                        fileExistsFlag = true;
                                                                    else
                                                                    {
                                                                        longFileName = "h:\\isdn\\sales\\" + fileName;
                                                                        if (File.Exists(longFileName))
                                                                            fileExistsFlag = true;
                                                                        else
                                                                        {
                                                                            longFileName = "h:\\isdn\\w\\" + fileName;
                                                                            if (File.Exists(longFileName))
                                                                                fileExistsFlag = true;
                                                                            else
                                                                            {
                                                                                longFileName = "h:\\isdn\\x\\" + fileName;
                                                                                if (File.Exists(longFileName))
                                                                                    fileExistsFlag = true;
                                                                                else
                                                                                {
                                                                                    longFileName = "h:\\isdn\\y\\" + fileName;
                                                                                    if (File.Exists(longFileName))
                                                                                        fileExistsFlag = true;
                                                                                    else
                                                                                    {
                                                                                        longFileName = "h:\\isdn\\z\\" + fileName;
                                                                                        if (File.Exists(longFileName))
                                                                                            fileExistsFlag = true;
                                                                                        else
                                                                                        {
                                                                                            longFileName = "i:\\isdn\\a\\" + fileName;
                                                                                            if (File.Exists(longFileName))
                                                                                                fileExistsFlag = true;
                                                                                            else
                                                                                            {
                                                                                                longFileName = "i:\\isdn\\b\\" + fileName;
                                                                                                if (File.Exists(longFileName))
                                                                                                    fileExistsFlag = true;
                                                                                                else
                                                                                                {
                                                                                                    longFileName = "i:\\isdn\\c\\" + fileName;
                                                                                                    if (File.Exists(longFileName))
                                                                                                        fileExistsFlag = true;
                                                                                                    else
                                                                                                    {
                                                                                                        longFileName = "i:\\isdn\\mbna\\" + fileName;
                                                                                                        if (File.Exists(longFileName))
                                                                                                            fileExistsFlag = true;
                                                                                                        else
                                                                                                        {
                                                                                                            longFileName = "i:\\isdn\\sales\\" + fileName;
                                                                                                            if (File.Exists(longFileName))
                                                                                                                fileExistsFlag = true;
                                                                                                            else
                                                                                                            {
                                                                                                                longFileName = "i:\\isdn\\w\\" + fileName;
                                                                                                                if (File.Exists(longFileName))
                                                                                                                    fileExistsFlag = true;
                                                                                                                else
                                                                                                                {
                                                                                                                    longFileName = "i:\\isdn\\x\\" + fileName;
                                                                                                                    if (File.Exists(longFileName))
                                                                                                                        fileExistsFlag = true;
                                                                                                                    else
                                                                                                                    {
                                                                                                                        longFileName = "i:\\isdn\\y\\" + fileName;
                                                                                                                        if (File.Exists(longFileName))
                                                                                                                            fileExistsFlag = true;
                                                                                                                        else
                                                                                                                        {
                                                                                                                            longFileName = "i:\\isdn\\z\\" + fileName;
                                                                                                                            if (File.Exists(longFileName))
                                                                                                                                fileExistsFlag = true;
                                                                                                                            else
                                                                                                                            {
                                                                                                                                longFileName = "z:\\isdn\\" + fileName;
                                                                                                                                if (File.Exists(longFileName))
                                                                                                                                    fileExistsFlag = true;
                                                                                                                                                                                                                            else
                                                                                                                                {
                                                                                                                                  longFileName = "j:\\isdn\\a\\" + fileName;
                                                                                                                                  if (File.Exists(longFileName))
                                                                                                                                    fileExistsFlag = true;
                                                                                                                                  else
                                                                                                                                  {
                                                                                                                                    longFileName = "j:\\isdn\\b\\" + fileName;
                                                                                                                                    if (File.Exists(longFileName))
                                                                                                                                        fileExistsFlag = true;
                                                                                                                                    else
                                                                                                                                    {
                                                                                                                                        longFileName = "j:\\isdn\\c\\" + fileName;
                                                                                                                                        if (File.Exists(longFileName))
                                                                                                                                            fileExistsFlag = true;
                                                                                                                                        else
                                                                                                                                        {
                                                                                                                                            longFileName = "j:\\isdn\\mbna\\" + fileName;
                                                                                                                                            if (File.Exists(longFileName))
                                                                                                                                                fileExistsFlag = true;
                                                                                                                                            else
                                                                                                                                            {
                                                                                                                                                longFileName = "j:\\isdn\\sales\\" + fileName;
                                                                                                                                                if (File.Exists(longFileName))
                                                                                                                                                    fileExistsFlag = true;
                                                                                                                                                else
                                                                                                                                                {
                                                                                                                                                    longFileName = "j:\\isdn\\w\\" + fileName;
                                                                                                                                                    if (File.Exists(longFileName))
                                                                                                                                                        fileExistsFlag = true;
                                                                                                                                                    else
                                                                                                                                                    {
                                                                                                                                                        longFileName = "j:\\isdn\\x\\" + fileName;
                                                                                                                                                        if (File.Exists(longFileName))
                                                                                                                                                            fileExistsFlag = true;
                                                                                                                                                        else
                                                                                                                                                        {
                                                                                                                                                            longFileName = "j:\\isdn\\y\\" + fileName;
                                                                                                                                                            if (File.Exists(longFileName))
                                                                                                                                                                fileExistsFlag = true;
                                                                                                                                                            else
                                                                                                                                                            {
                                                                                                                                                                longFileName = "j:\\isdn\\z\\" + fileName;
                                                                                                                                                                if (File.Exists(longFileName))
                                                                                                                                                                    fileExistsFlag = true;
                                                                                                                                                                else
                                                                                                                                                                {
                                                                                                                                                                    longFileName = "k:\\isdn\\a\\" + fileName;
                                                                                                                                                                    if (File.Exists(longFileName))
                                                                                                                                                                        fileExistsFlag = true;
                                                                                                                                                                    else
                                                                                                                                                                    {
                                                                                                                                                                        longFileName = "k:\\isdn\\b\\" + fileName;
                                                                                                                                                                        if (File.Exists(longFileName))
                                                                                                                                                                            fileExistsFlag = true;
                                                                                                                                                                        else
                                                                                                                                                                        {
                                                                                                                                                                            longFileName = "k:\\isdn\\c\\" + fileName;
                                                                                                                                                                            if (File.Exists(longFileName))
                                                                                                                                                                                fileExistsFlag = true;
                                                                                                                                                                            else
                                                                                                                                                                            {
                                                                                                                                                                                longFileName = "k:\\isdn\\mbna\\" + fileName;
                                                                                                                                                                                if (File.Exists(longFileName))
                                                                                                                                                                                    fileExistsFlag = true;
                                                                                                                                                                                else
                                                                                                                                                                                {
                                                                                                                                                                                    longFileName = "k:\\isdn\\sales\\" + fileName;
                                                                                                                                                                                    if (File.Exists(longFileName))
                                                                                                                                                                                        fileExistsFlag = true;
                                                                                                                                                                                    else
                                                                                                                                                                                    {
                                                                                                                                                                                        longFileName = "k:\\isdn\\w\\" + fileName;
                                                                                                                                                                                        if (File.Exists(longFileName))
                                                                                                                                                                                            fileExistsFlag = true;
                                                                                                                                                                                        else
                                                                                                                                                                                        {
                                                                                                                                                                                            longFileName = "k:\\isdn\\x\\" + fileName;
                                                                                                                                                                                            if (File.Exists(longFileName))
                                                                                                                                                                                                fileExistsFlag = true;
                                                                                                                                                                                            else
                                                                                                                                                                                            {
                                                                                                                                                                                                longFileName = "k:\\isdn\\y\\" + fileName;
                                                                                                                                                                                                if (File.Exists(longFileName))
                                                                                                                                                                                                    fileExistsFlag = true;
                                                                                                                                                                                                else
                                                                                                                                                                                                {
                                                                                                                                                                                                    longFileName = "k:\\isdn\\z\\" + fileName;
                                                                                                                                                                                                    if (File.Exists(longFileName))
                                                                                                                                                                                                        fileExistsFlag = true;
                                                                                                                                                                                                    else
                                                                                                                                                                                                    {
                                                                                                                                                                                                        longFileName = "l:\\isdn\\b\\" + fileName;
                                                                                                                                                                                                        if (File.Exists(longFileName))
                                                                                                                                                                                                            fileExistsFlag = true;
                                                                                                                                                                                                        else
                                                                                                                                                                                                        {
                                                                                                                                                                                                            longFileName = "l:\\isdn\\c\\" + fileName;
                                                                                                                                                                                                            if (File.Exists(longFileName))
                                                                                                                                                                                                                fileExistsFlag = true;
                                                                                                                                                                                                            else
                                                                                                                                                                                                            {
                                                                                                                                                                                                                longFileName = "l:\\isdn\\mbna\\" + fileName;
                                                                                                                                                                                                                if (File.Exists(longFileName))
                                                                                                                                                                                                                    fileExistsFlag = true;
                                                                                                                                                                                                                else
                                                                                                                                                                                                                {
                                                                                                                                                                                                                    longFileName = "l:\\isdn\\sales\\" + fileName;
                                                                                                                                                                                                                    if (File.Exists(longFileName))
                                                                                                                                                                                                                        fileExistsFlag = true;
                                                                                                                                                                                                                    else
                                                                                                                                                                                                                    {
                                                                                                                                                                                                                        longFileName = "l:\\isdn\\w\\" + fileName;
                                                                                                                                                                                                                        if (File.Exists(longFileName))
                                                                                                                                                                                                                            fileExistsFlag = true;
                                                                                                                                                                                                                        else
                                                                                                                                                                                                                        {
                                                                                                                                                                                                                            longFileName = "l:\\isdn\\x\\" + fileName;
                                                                                                                                                                                                                            if (File.Exists(longFileName))
                                                                                                                                                                                                                                fileExistsFlag = true;
                                                                                                                                                                                                                            else
                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                longFileName = "l:\\isdn\\y\\" + fileName;
                                                                                                                                                                                                                                if (File.Exists(longFileName))
                                                                                                                                                                                                                                    fileExistsFlag = true;
                                                                                                                                                                                                                                else
                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                    longFileName = "l:\\isdn\\z\\" + fileName;
                                                                                                                                                                                                                                    if (File.Exists(longFileName))
                                                                                                                                                                                                                                        fileExistsFlag = true;
                                                                                                                                                                                                                                   else
                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                        longFileName = "m:\\isdn\\b\\" + fileName;
                                                                                                                                                                                                                                        if (File.Exists(longFileName))
                                                                                                                                                                                                                                            fileExistsFlag = true;
                                                                                                                                                                                                                                        else
                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                            longFileName = "m:\\isdn\\c\\" + fileName;
                                                                                                                                                                                                                                            if (File.Exists(longFileName))
                                                                                                                                                                                                                                                fileExistsFlag = true;
                                                                                                                                                                                                                                            else
                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                longFileName = "m:\\isdn\\mbna\\" + fileName;
                                                                                                                                                                                                                                                if (File.Exists(longFileName))
                                                                                                                                                                                                                                                    fileExistsFlag = true;
                                                                                                                                                                                                                                                else
                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                    longFileName = "m:\\isdn\\sales\\" + fileName;
                                                                                                                                                                                                                                                    if (File.Exists(longFileName))
                                                                                                                                                                                                                                                        fileExistsFlag = true;
                                                                                                                                                                                                                                                    else
                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                        longFileName = "m:\\isdn\\w\\" + fileName;
                                                                                                                                                                                                                                                        if (File.Exists(longFileName))
                                                                                                                                                                                                                                                            fileExistsFlag = true;
                                                                                                                                                                                                                                                        else
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                            longFileName = "m:\\isdn\\x\\" + fileName;
                                                                                                                                                                                                                                                            if (File.Exists(longFileName))
                                                                                                                                                                                                                                                                fileExistsFlag = true;
                                                                                                                                                                                                                                                            else
                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                longFileName = "m:\\isdn\\y\\" + fileName;
                                                                                                                                                                                                                                                                if (File.Exists(longFileName))
                                                                                                                                                                                                                                                                    fileExistsFlag = true;
                                                                                                                                                                                                                                                                else
                                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                                    longFileName = "m:\\isdn\\z\\" + fileName;
                                                                                                                                                                                                                                                                    if (File.Exists(longFileName))
                                                                                                                                                                                                                                                                        fileExistsFlag = true;
                                                                                                                                                                                                                                                                    else
                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                        longFileName = "m:\\isdn\\a\\" + fileName;
                                                                                                                                                                                                                                                                        if (File.Exists(longFileName))
                                                                                                                                                                                                                                                                            fileExistsFlag = true;
                                                                                                                                                                                                                                                                        else
                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                            longFileName = "l:\\isdn\\a\\" + fileName;
                                                                                                                                                                                                                                                                            if (File.Exists(longFileName))
                                                                                                                                                                                                                                                                                fileExistsFlag = true;
                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                }
                                                                                                                                                                                                                            }
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                    }
                                                                                                                                                                                                                }
                                                                                                                                                                                                            }
                                                                                                                                                                                                        }
                                                                                                                                                                                                    }
                                                                                                                                                                                                }
                                                                                                                                                                                            }
                                                                                                                                                                                        }
                                                                                                                                                                                    }
                                                                                                                                                                                }
                                                                                                                                                                            }
                                                                                                                                                                        }
                                                                                                                                                                    }
                                                                                                                                                                }
                                                                                                                                                            }
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                  }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if (fileExistsFlag)
            return longFileName;
        else
            return "NOTFOUND";
    }
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!