@Spunny , Welcome to Microsoft Q&A, based on your description, you want to make some checking and then return the correspond result.
I make a code example and you could have a look.
internal class Program
{
static void Main(string[] args)
{
string path1 = "C:\\Temp\\test.txt;
string path2 = "C:\\ParentFolder\\ChildFolder";
Checkpath check1 = GetInfo(path1);
Checkpath check2 = GetInfo(path2);
}
static Checkpath GetInfo(string path)
{
Checkpath checkpath = new Checkpath();
try
{
FileAttributes attr = File.GetAttributes(path);
//detect whether its a directory or file
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
checkpath.type = Filetype.Dir;
else
checkpath.type = Filetype.File;
checkpath.Ifexists= true;
}
catch
{
bool t = Path.HasExtension(path);
if (t)
{
checkpath.type = Filetype.File;
}
else
{
checkpath.type = Filetype.Dir;
}
checkpath.Ifexists = false;
}
return checkpath;
}
}
public class Checkpath
{
public bool Ifexists { get; set; }
public Filetype type { get; set; }
}
public enum Filetype
{
File = 0,
Dir=1
}
Note: I also agree with Viorel-1's advice, the above works in most cases but not in special cases.
Hope my code could help you.
Best Regards,
Jack