none
Check directory states the such directory doesn't exist even when it does RRS feed

  • Question

  • I need a part of my program to check if a folder or directory exists but every time it says the directory doesn't exist even when it does. The line that does not work is 69

    Thanks,

    Griffin

    EDIT: It says that folder access is denied. Any ideas?

    My code:

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; using System.IO; using System.IO.Compression; namespace ConsoleApp2 { class Program { static void Main(string[] args) { Console.Title = "cool"; goto start; start: Console.WriteLine(""); goto cool; cool: Console.WriteLine("Type UnZip to make a compressed folder and Zip to make a compressed folder."); var testString = Console.ReadLine(); var finalString = testString.ToLower(); if (testString == "zip") { goto zip; } else if (testString == "unzip") { goto unzip; } else if (testString == "end") { goto end; } else { Console.WriteLine(testString + " is invalid. Please type Zip or Unzip."); goto cool; } unzip: Console.WriteLine("Type the path to decompress"); var path = @Console.ReadLine(); if (File.Exists(path)) { Console.WriteLine("Directory Exists."); goto unzipcont; } else { Console.WriteLine(path + " does not exist. "+"Please type a direcotry that exits."); goto unzip; } unzipcont: var cool = Directory.GetParent(path); var fullpathtoextractto = cool + "\\" + Path.GetFileNameWithoutExtension(path); Console.WriteLine("Extracting files to path " + fullpathtoextractto); ZipFile.ExtractToDirectory(path,fullpathtoextractto); System.Diagnostics.Process.Start(fullpathtoextractto); goto end; zip: Console.WriteLine("Type the folder to compress"); var zippath = @Console.ReadLine(); if (File.Exists(zippath)) { Console.WriteLine("Directory Exists."); goto zipcont; } else { Console.WriteLine(zippath + " does not exist. " + "Please type a direcotry that exits."); goto zip; } zipcont: var cooltwo = Directory.GetParent(zippath); var fullpathtoextracttotwo = cooltwo + "\\" + Path.GetFileNameWithoutExtension(zippath); Console.WriteLine("Infusing files to path " + fullpathtoextracttotwo); ZipFile.CreateFromDirectory(zippath, fullpathtoextracttotwo); System.Diagnostics.Process.Start(fullpathtoextracttotwo); goto end; end: Console.WriteLine("Type another the unzip or zip another folder. Type quit to exit the program."); var makeorbrakexd = Console.ReadLine(); var actualvar = makeorbrakexd.ToLower(); if (actualvar == "another") { Console.Clear(); goto start; } else if (actualvar == "quit") { Environment.Exit(1); } else { Console.WriteLine(makeorbrakexd + " is invalid. Please type another or quit."); goto end; } }



    • Edited by Garlfin Saturday, September 15, 2018 7:31 PM edit
    Saturday, September 15, 2018 7:06 PM

Answers

  • You are doing things such as this:

    if (File.Exists(path)) { Console.WriteLine("Directory Exists."); ...

    You should note that if "path" refers to a directory instead of a file, then File.Exists will not work on it. For directories you should use Directory.Exists instead of File.Exists.

    Even if you use Directory.Exists, there is another reason why it can return "false": The name "Exists" is misleading -- it doesn't actually check that the directory exists, what it checks is that it is accessible. Therefore it returns false if you don't have permissions on the folder, even if the folder does exist.
               

    • Marked as answer by Garlfin Sunday, September 16, 2018 12:21 AM
    Saturday, September 15, 2018 7:58 PM
    Moderator

All replies

  • You are doing things such as this:

    if (File.Exists(path)) { Console.WriteLine("Directory Exists."); ...

    You should note that if "path" refers to a directory instead of a file, then File.Exists will not work on it. For directories you should use Directory.Exists instead of File.Exists.

    Even if you use Directory.Exists, there is another reason why it can return "false": The name "Exists" is misleading -- it doesn't actually check that the directory exists, what it checks is that it is accessible. Therefore it returns false if you don't have permissions on the folder, even if the folder does exist.
               

    • Marked as answer by Garlfin Sunday, September 16, 2018 12:21 AM
    Saturday, September 15, 2018 7:58 PM
    Moderator
  • How can I give the program permission to compress the file?
    Sunday, September 16, 2018 12:24 AM
  • I keep on getting a error that there is no permission to read and write the file

    Sunday, September 16, 2018 12:58 AM
  • How can I give the program permission to compress the file?

    You can run the program with elevated privileges. First try it "manually" by right-clicking on the compiled executable and selecting "Run as Administrator". If this works, you can change your program so that it always asks for elevation by adding an app.manifest file. Visual Studio will offer this option if you select "add new item" from the Project. Then edit the app.manifest and use the option "requireAdministrator" (you will see it commented out inside the file that Visual Studio generated). After doing this, every time you run the program it will gain administrative privileges, possibly prompting for the password if it needs it.

    You should be aware that even if the program runs as Administrator there might be some folders that don't even have permissions for Administrators. To access such a folder you would need to acquire ownership and give permissions to yourself, but you want to be careful with this; changing the ownership of a system folder may break some functionality or create security vulnerabilities in Windows.

    Sunday, September 16, 2018 6:46 AM
    Moderator
  • The folder I am trying to compress is in my downloads folder and ran the program as admin but i am still getting access errors.
    • Edited by Garlfin Sunday, September 16, 2018 2:01 PM
    Sunday, September 16, 2018 1:59 PM
  • Which antivirus you use? 

    Sincerely, Highly skilled coding monkey.

    Sunday, September 16, 2018 3:56 PM
  • Sorry for the painfully long response, but I currently am using just windows defender. No dedicated antivirus.
    Tuesday, September 18, 2018 11:04 PM
  • Do it simple way:

    1. Create new folder under c:\ - C:\00

    1.2. Create new folder under C:\00 - C:\00\ToBeCompressed

    1.3. Put some files into C:\00\ToBeCompressed

    2. Make one row program - ZipFile.CreateFromDirectory(@"C:\00\ToBeCompressed", @"C:\00\filename.zip");

    3. Run program.

    If it do all things properly - there is an error in your path/filename management or there are a problem with permission to access one-folder-up from your downloads.


    Sincerely, Highly skilled coding monkey.

    Wednesday, September 19, 2018 8:05 AM
  • I also tried it on my desktop and it doesn't work. I think i'm just gonna rewrite the program. Thanks for any help with my problems.

    Sincerely, Griffin


    Griffin

    Wednesday, September 19, 2018 12:09 PM