C # .NET is a remote path to a directory or file

How to determine if a remote path was a file or directory using C # .Net?

thanks

+3
source share
6 answers

Assuming the file / directory really exists, you can use two static methods:

Both take one string argument and return a boolean if the file / directory exists.

- , / - , - " /" /, , () . ", " ", " . :

  • A: ( )
  • B:

:

  • c:\windows\ -
  • c:\windows - B
  • c:\windows\notepad.exe -
  • c:\windows\system32\drivers\etc\hosts - A

, , , .

+10

Directory.Exists(...) , false. , File.Exists(...) , false

+4

: path.GetFileName

, Empty, , , .

+2

, , , , "myfile.txt". , , , .

File.Exists Directory.Exists GetFileAttributesEx, , , . , - .

+1

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetError = true)]
static extern int GetFileAttributes(string lpFileName);

bool IsDirectory(string path) {
     return GetFileAttributes(path) & 16 == 16;
}

Directory.Exists File.Exists, , .

. .

+1
if (Path.HasExtension(fileFolderPath) && !string.IsNullOrEmpty(Path.GetFileName(fileFolderPath)))
{
    //path is for file
}

Directory.Exists File.Exists , .

+1

Source: https://habr.com/ru/post/1716965/


All Articles