0

Good day, I just wanted to ask how to check if folder exist on my project's folder. I already have a folder named Temp_File.

this is what I've tried but didn't work.

        if(File.Exists("Temp_Folder"))
        {
          Messagebox.Show("Folder exist");
        }

I put this code in my Form1_load. Thank you~

19GreenBlankets
  • 95
  • 1
  • 12
  • 3
    I think you have to have the file extension in the string. `"Temp_File.txt"` – Jun Kang Jul 13 '17 at 21:58
  • Also, this only works if the file is located in the current working directory of the application – Mathias R. Jessen Jul 13 '17 at 21:59
  • Sir, do you mean that I need to make my file existing on my project folder? I already did sir. – 19GreenBlankets Jul 13 '17 at 22:00
  • 1
    Possible duplicate of [C# class to check if files exists in folder](https://stackoverflow.com/questions/8022796/c-sharp-class-to-check-if-files-exists-in-folder) – mrogers Jul 13 '17 at 22:00
  • Similar to that question ^, but this would use a `FileInfo` – ps2goat Jul 13 '17 at 22:23
  • 1
    The file needs to exist in your `bin/Debug` folder, or whereever the built application is going. Not your project source folder. – Blorgbeard Jul 13 '17 at 22:24
  • 1
    If the file is part of your project in VS solution explorer, make sure its properties are set to "Build Action = Content" and "Copy to Output Directory = Copy If Newer". Otherwise it's not getting copied to the folder where the application is run from, and thus not found. – Dax Fohl Jul 13 '17 at 22:30
  • 1
    do you mean folder exists or file. The title says file but the text says folder – pm100 Jul 13 '17 at 23:12
  • Folder sir. thank you for your reply. – 19GreenBlankets Jul 13 '17 at 23:23
  • Possible duplicate of [If a folder does not exist, create it](https://stackoverflow.com/questions/9065598/if-a-folder-does-not-exist-create-it) – Jon Schneider Jul 17 '17 at 19:47

3 Answers3

0

File.Exists is checking file, not a folder, also you need to provide a full physical path for file like @"C:\mydata\TempFile.txt" to work. I totally confused if you want to check for file or folder. If you are testing for folder=> use Directory.GetDirectories it will return an array of all subdirectories. If you are testing for file=>relative file will be save in [Solution_Path][Project_Name][filename.extention] I recommend you using below code to verify you looking to correct path or using an absolute path.

var test_file=File.Create("test.txt");
Console.WriteLine(test_file.Name);
Navid Golforoushan
  • 728
  • 1
  • 9
  • 16
0

if the file you're looking for is in the same place as your executable, then do the following:

namespace name
{
    class TestClass
    {
        public const string ConstBackSlash = "\\"; // The constant back slash
        public string GetApplicationExecutableDirectoryName()
        {
            return Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
        }

        public void Test()
        {
            if (File.Exists(GetApplicationExecutableDirectoryName() + ConstBackSlash + "Temp_File"))
            {
                MessageBox.Show("File exists");
            }
        }
    }
}

otherwise, get the directory path using the common file dialog.

Max Euwe
  • 431
  • 3
  • 9
0

I guess it depends on what you mean by "the current folder". Presumably you mean the folder in which your application was installed. If that's the case, then you can get the directory that your application lives in using this (note that you will need a reference to System.Reflection to use the Assembly class):

var thisExeDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

Then, when you have that directory, you can search for folders underneath it using Directory.EnumerateDirectories, passing in our exe path as the directory to search under, the directoryToFind as the SearchPattern, and SearchOption.AllDirectories if you want to search sub-folders as well:

var directoryToFind = "Temp_Folder";

var result = Directory
    .EnumerateDirectories(thisExeDirectory, directoryToFind, SearchOption.AllDirectories)
    .FirstOrDefault();

if (result != null)
{
    Console.WriteLine($"Found directory here:\n\"{result}\"");
}

// Wait for input before closing
Console.WriteLine("\nDone!\nPress any key to exit...");
Console.ReadKey();

Output

enter image description here


If you were actually looking for a File instead of a Directory, the code is mostly the same, except you'd use the Directory.EnumerateFiles instead:

var fileToFind = "TextFile1.txt";

var result = Directory
    .EnumerateFiles(thisExeDirectory, fileToFind, SearchOption.AllDirectories)
    .FirstOrDefault();    
Rufus L
  • 36,127
  • 5
  • 30
  • 43