Check if path is directory or file C# -


i tried check if path deleted directory or file path directory or file. found code:

fileattributes attr = file.getattributes(@"c:\example"); if (attr.hasflag(fileattributes.directory))     messagebox.show("it's directory"); else     messagebox.show("it's file"); 

however code not working deleted directory or file.

i have 2 folders

c:\dir1 c:\dir2 

in dir1 there normal files "test.txt", in dir2 there compressed files "test.rar" or "test.zip" , need delete file in dir2 when file in dir1 deleted.

something tried, nothing works.

is possible achieve this?

thanks!

if object represented path not exist or has been deleted file system, you've got string representing file system path: it's not anything.

the normal convention indicating path intended directory (rather file) terminate directory separator, so

c:\foo\bar\baz\bat 

is taken indicate file, while

c:\foo\bar\baz\bat\ 

is taken indicate directory.

if want delete file system entry (either file, or directory, recursively deleting contents , subdirectories), should suffice:

public void deletefileordirectory( string path ) {    try   {     file.delete( path ) ;   }   catch ( unauthorizedaccessexception )   {     // if here,     // - caller lacks required permissions, or     // - file has read-only attribute set, or     // - file directory.     //     // either way: intentionally swallow exception , continue.   }    try   {     directory.delete( path , true ) ;   }   catch ( directorynotfoundexception )   {     // if here,     // - path not exist or not found     // - path refers file instead of directory     // - path invalid (e.g., on unmapped drive or like)     //     // either way: intentationally swallow exception , continue   }    return ; } 

one should note there number of exceptions can thrown during process.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -