I am trying to write some C# code that will allow me to determine if the
current user running my application has "write" permissions to a particular
folder. However, I can't seem to get it to work. In the code snippet I've
included below, it always returns "true" whether the folder/file exists or
not (let alone whether or not they have permissions).
Any ideas?
<<< BEGIN CODE SNIPPET >>>
using System;
using System.Security;
using System.Security.Permissions;
public class MyClass {
public static void Main() {
// The filename passed in below does not exist (drive Z doesn't even exist
on my system),
// however, it still returns "true".
Console.WriteLine("z:\\testthisout.txt Permission: " +
HasWritePermissions("z:\\testthisout.txt").ToStrin g());
}
private static bool CheckPermission(FileIOPermission RequestedPermission) {
try {
RequestedPermission.Demand();
}
catch {
return false;
}
return true;
}
private static bool HasWritePermissions(string fileName) {
System.Security.Permissions.FileIOPermission oPerm = new
System.Security.Permissions.FileIOPermission(
System.Security.Permissions.FileIOPermissionAccess .Write, fileName);
return CheckPermission(oPerm);
}
}
<<< END CODE SNIPPET >>>
--- TIA, Jeff