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.WriteLi ne("z:\\testthi sout.txt Permission: " +
HasWritePermiss ions("z:\\testt hisout.txt").To String());
}
private static bool CheckPermission (FileIOPermissi on RequestedPermis sion) {
try {
RequestedPermis sion.Demand();
}
catch {
return false;
}
return true;
}
private static bool HasWritePermiss ions(string fileName) {
System.Security .Permissions.Fi leIOPermission oPerm = new
System.Security .Permissions.Fi leIOPermission(
System.Security .Permissions.Fi leIOPermissionA ccess.Write, fileName);
return CheckPermission (oPerm);
}
}
<<< END CODE SNIPPET >>>
--- TIA, Jeff
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.WriteLi ne("z:\\testthi sout.txt Permission: " +
HasWritePermiss ions("z:\\testt hisout.txt").To String());
}
private static bool CheckPermission (FileIOPermissi on RequestedPermis sion) {
try {
RequestedPermis sion.Demand();
}
catch {
return false;
}
return true;
}
private static bool HasWritePermiss ions(string fileName) {
System.Security .Permissions.Fi leIOPermission oPerm = new
System.Security .Permissions.Fi leIOPermission(
System.Security .Permissions.Fi leIOPermissionA ccess.Write, fileName);
return CheckPermission (oPerm);
}
}
<<< END CODE SNIPPET >>>
--- TIA, Jeff