Check if folder has Write Permission or not in C#

 

//  Checks the ability to create and write to a file in the supplied directory and  returns True if successful; otherwise false.

public static bool HasFolderWritePermission(string directoryPath)

{

var success = false;

const string TEMP_FILE = “\tempFile.tmp”;

var fullPath = directoryPath + TEMP_FILE;

if (Directory.Exists(directoryPath))

{

try

{

using (var fs = new FileStream(fullPath, FileMode.CreateNew, FileAccess.Write))

{

fs.WriteByte(0xff);

}

if (File.Exists(fullPath))

{

File.Delete(fullPath);

success = true;

}

}

catch (Exception)

{

success = false;

}

}

return success;

}

 

Written by 

Leave a Reply

Your email address will not be published. Required fields are marked *