0

c#のフォルダーに特定のフォルダーが存在するかどうかを確認したい。現在、次のコードを使用しています。

        string currentPath = Directory.GetCurrentDirectory();

        foreach (string subDir in Directory.GetDirectories(currentPath))
        {
            if (subDir == currentPath + @"\Database") // if "Database" folder exists
            {
                dbinRoot = true;
            }
        }
        if (dbinRoot == true)
        {
            currentPath = currentPath + @"\Database\SMAStaff.accdb";
        }
        else
        {
            for (int i = 0; i < 2; i++)
            {
                currentPath = currentPath.Remove(currentPath.LastIndexOf("\\"));
            }
            currentPath = currentPath + "\\Database\\SMAStaff.accdb";
        }

これを行うためのより良い方法があるかどうか知りたいですか?

4

3 に答える 3

4

使用できます:

Directory.Exists(currentPath + @"\Database")
于 2012-10-29T06:39:21.810 に答える
1

Path.Combineまた、メソッドを使用してパスの一部を連結することもより確実になります

if(Directory.Exists(Path.Combine(Directory.GetCurrentDirectory()、@ "Database"))){}

于 2012-10-29T06:47:47.580 に答える
0

あなたはおそらく直接探しています

currentPath = Directory.GetCurrentDirectory();
bool fileExists = File.Exists(Path.Combine(currentPath, "Database\\SMAStaff.accdb")))

また、forサイクルには、読みやすさを向上させるために含まれている場合があります

currentPath = Directory.GetParent(currentPath).FullName;
于 2012-10-29T07:01:59.157 に答える