المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Check if user can write and delete from a directory



C# Programming
07-26-2010, 06:53 AM
In a Windows (C#) form, I need to check if the user has the required permissions to write and delete from a directory (that they select).
I have some code that will do the job, but it feels wrong.
I try writing and deleting a test file and if an exception is thrown, then the test fails.
I am a bit unhappy with using exception testing like this, but can't find any other method that does not require reams of code.

Does anyone have another method that does not require exception testing?

Here is my code.
WriteToLog(string Message, Enum LogType) is a general logging method that writes the first parameter to the log file identified by the second parameter.
private bool UserCanWrite()
{
try
{
WriteToLog("Test", LogFiles.Test);

string TestLogPath = "Failed.Log";

if (BaseDirectory.Length == 0)
{
TestLogPath = string.Format("C:\\{0}", TestLogPath);
}
else {
TestLogPath = string.Format("{0}\\{1}", BaseDirectory, TestLogPath);
}

FileInfo TestLog = new FileInfo(TestLogPath);
TestLog.Delete();

return true;
}

catch
{
MessageBox.Show("You Don't have Read, Write and Delete privileges on the selected Base Directory\n\nPlease have your System Administrator grant those privileges before proceeding", "Higher Level of Privileges Needed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}