Look for file in subdirectoy

I know that when you create a .txt file or open one, without specifying a directory, that a c# program will look in its Debug directory for that file, or whatever directory the .exe happens to be in.. i think. How do I tell it to look in a subdirectoy of that debug directory I do not want to specify an exact location like "c:\dirctory\sub-directory"

I just want it to look in a subdirectory in the default directory. I tried tellin it  to open the file like this "\subdirectory\file.txt" but that did not work. Any help thanks...



Answer this question

Look for file in subdirectoy

  • FrankyLi

    try this:

    if (File.Exists("subdirectory\\file.txt")) {..}
    or
    FileInfo fi = new FileInfo("subdirectory\\file.txt");

    The extra "\" is because of the fact that in C#, the "\" character is used as an escape character for various escape sequences like \n, \r, etc.

    hope that helps,
    Imran.

  • SfinXx

    yep, that worked.. thanks for the help
  • BY AU

    The default dir may be the dir where the .exe or .dll placed or working directory.

    In order to get the dir where your assembly resides and then check for sub-dir you can do the following:

    string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

    Then you can append any sub-dir you want.

    string newDir = Path.Combine(dir, "subfolder");

    if ( Directory.Exists(newDir))

    //do the job

    else

    //no such dir


  • Look for file in subdirectoy