Home > Back-end >  Try to find folder name "temp.dll" by using Directory.Exsits()
Try to find folder name "temp.dll" by using Directory.Exsits()

Time:11-01

I'm having a problem with what i asked on the title. In my program, I'm trying to wrap a dll file with folder. When I made the folder name as same as file name and try to find existance with Directory.Exists func... it doesn't work.

[Detail Example]

string fileName = "C:\User\Installprogram\Temp.dll"   //<- "Temp.dll" is Directory
if (!Directory.Exists(fileName))
   return false;

I double checked if the directory is in proper place. Weird point is other folder names properly return true with above example. I'm sort of guessing if folder name contains ".dll" making unable to catch directory by Directory.Exists func.

Help me

CodePudding user response:

I have tried it with following code and it worked as expected. So you should provide more details. Maybe my code helps already to fix your issue, that's why i post it as an answer:

string folder = @"C:\User\Installprogram\Temp.dll";
string file = @"C:\User\Installprogram\Temp.dll\Temp.dll";
Directory.CreateDirectory(folder);
File.WriteAllText(file, "temp"); // not a dll but text-file, but should not make a difference
Console.WriteLine($"Folder exists: {Directory.Exists(folder)}"); // true

CodePudding user response:

This code only checks if a directory exists but is not a file. In your case, you are checking for the existence of a file with the wrong method. According to Microsoft documentation, we have:

Determines whether the given path refers to an existing directory on disk.

Consider using the static Exists() method of the File class, this one checks for the existence of a file on disk;

Determines whether the specified file exists.

References:

  • Related