检查在.NET中的目录和文件的写权限 [英] Checking for directory and file write permissions in .NET

查看:135
本文介绍了检查在.NET中的目录和文件的写权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.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;
        }
    }
 

解决方案

通过理查德和<一个答案href="http://stackoverflow.com/questions/1281620/checking-for-directory-and-file-write-permissions-in-net/1281661#1281661">Jason是那种在正确的方向。但是你应该做的是<一个href="http://groups.google.co.uk/group/microsoft.public.dotnet.languages.csharp/msg/208347aa99a2de3b"相对=nofollow>计算有效权限的运行您的code中的用户身份。无上述正确的例子解释,例如组成员身份。

我是pretty的肯定<一href="https://web.archive.org/web/20090226231529/http://alt.pluralsight.com/wiki/default.aspx/Pluralsight/KeithBrown.html"相对=nofollow>基思布朗有一些code,为此在他的<一个href="https://web.archive.org/web/20090221160820/http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook.HomePage"相对=nofollow>的<一个wiki版本(下线在这个时候) href="https://web.archive.org/web/20090221160820/http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook.HomePage"相对=nofollow>的.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.

这篇关于检查在.NET中的目录和文件的写权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆