检查在.NET中的目录和文件的写权限权限、文件、目录、NET

2023-09-02 21:07:59 作者:擦不掉的是回忆

在我的.NET 2.0的应用程序,我需要检查是否有足够的权限存在则创建并写入文件的目录。为此,我有以下函​​数试图创建一个文件,写一个字节给它,删除自己事后测试权限确实存在。

我想检查的最好办法就是实际尝试并做到这一点,捕捉发生的任何异常。我不是特别高兴的一般例外抓了,所以是有这样做的更好或者更接受的方式?

 私人常量字符串TEMP_FILE =\ tempFile.tmp;

///<总结>
///检查创建并写入到所提供的目录中的文件的能力。
///< /总结>
///< PARAM NAME =目录>字符串重新presenting的目录路径检查和LT; /参数>
///<返回>真如果成功;否则为false< /回报>
私有静态布尔CheckDirectoryAccess(字符串目录)
{
    布尔成功= FALSE;
    字符串FULLPATH =目录+ TEMP_FILE;

    如果(Directory.Exists(目录))
    {
        尝试
        {
            使用(的FileStream FS =新的FileStream(FULLPATH,FileMode.CreateNew,
                                                            FileAccess.Write))
            {
                fs.WriteByte(0xFF的);
            }

            如果(File.Exists(FULLPATH))
            {
                File.Delete(完整路径);
                成功= TRUE;
            }
        }
        赶上(例外)
        {
            成功= FALSE;
        }
    }
 

解决方案

通过理查德和Jason是那种在正确的方向。但是你应该做的是计算有效权限的运行您的code中的用户身份。无上述正确的例子解释,例如组成员身份。

我是pretty的肯定基思布朗有一些code,为此在他的的的.NET开发人员指南Windows安全。这也是合理的细节在他的编程Windows安全本书中讨论。

计算有效权限是不是一件轻松的事情,你的code,试图创建一个文件和追赶的安全性引发的异常可能是阻力最小的路径。

怎样修改系统临时文件目录

In my .NET 2.0 application, I need to check if sufficient permissions exist to create and write to files to a directory. To this end, I have the following function that attempts to create a file and write a single byte to it, deleting itself afterwards to test that permissions do exist.

I figured the best way to check was to actually try and do it, catching any exceptions that occur. I'm not particularly happy about the general Exception catch though, so is there a better or perhaps a more accepted way of doing this?

private const string TEMP_FILE = "\tempFile.tmp";

/// <summary>
/// Checks the ability to create and write to a file in the supplied directory.
/// </summary>
/// <param name="directory">String representing the directory path to check.</param>
/// <returns>True if successful; otherwise false.</returns>
private static bool CheckDirectoryAccess(string directory)
{
    bool success = false;
    string fullPath = directory + TEMP_FILE;

    if (Directory.Exists(directory))
    {
        try
        {
            using (FileStream fs = new FileStream(fullPath, FileMode.CreateNew, 
                                                            FileAccess.Write))
            {
                fs.WriteByte(0xff);
            }

            if (File.Exists(fullPath))
            {
                File.Delete(fullPath);
                success = true;
            }
        }
        catch (Exception)
        {
            success = false;
        }
    }

解决方案

The answers by Richard and Jason are sort of in the right direction. However what you should be doing is computing the effective permissions for the user identity running your code. None of the examples above correctly account for group membership for example.

I'm pretty sure Keith Brown had some code to do this in his wiki version (offline at this time) of The .NET Developers Guide to Windows Security. This is also discussed in reasonable detail in his Programming Windows Security book.

Computing effective permissions is not for the faint hearted and your code to attempt creating a file and catching the security exception thrown is probably the path of least resistance.

 
精彩推荐
图片推荐