相关疑难解决方法(0)

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

在我的.NET 2.0应用程序中,我需要检查是否有足够的权限来创建和写入目录的文件.为此,我有以下函数尝试创建一个文件并向其写入一个字节,然后删除自己以测试权限是否存在.

我认为检查的最佳方法是实际尝试并执行此操作,捕获发生的任何异常.虽然我对一般的异常捕获并不是特别高兴,所以有更好的或者更可接受的方式吗?

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 …
Run Code Online (Sandbox Code Playgroud)

.net c# directory file-permissions winforms

75
推荐指数
5
解决办法
11万
查看次数

如何在.NET Core中修改文件访问控制

我正在尝试更改.NET Core中文件的权限.但是,似乎FileInfo已经没有SetAccessControl了.

// Create a new FileInfo object.
FileInfo fInfo = new FileInfo(FileName);

// Get a FileSecurity object that represents the 
// current security settings.
FileSecurity fSecurity = fInfo.GetAccessControl();

// Add the FileSystemAccessRule to the security settings. 
fSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                                                Rights,
                                                ControlType));

// Set the new access settings.
fInfo.SetAccessControl(fSecurity);
Run Code Online (Sandbox Code Playgroud)

目标只是将执行权添加到文件的当前所有者(不是Windows或Unix特定功能).

有关如何在.NET Core上执行此操作的任何线索?

c# file-permissions .net-core

16
推荐指数
4
解决办法
6161
查看次数

标签 统计

c# ×2

file-permissions ×2

.net ×1

.net-core ×1

directory ×1

winforms ×1