【问题标题】:Remove read only attribute from file while working on web application在处理 Web 应用程序时从文件中删除只读属性
【发布时间】:2013-06-08 05:27:45
【问题描述】:

在 c# 中处理 Web 应用程序时如何从文件中删除只读属性。同样,我可以通过使用此代码在 Windows 应用程序上执行此操作。

FileInfo file = new FileInfo(@"D:\Test\Sample.zip");
if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
    file.IsReadOnly = false;
}

我在 Web 应用程序中尝试的相同代码,它没有删除只读属性。请帮我解决同样的问题。

【问题讨论】:

  • 您对该文件夹有适当的权限吗?
  • 我猜区别在于用户权限:Win App 用户可能拥有比您的 Web App Pool 用户更高的权限。

标签: c# attributes


【解决方案1】:

如果您的应用程序写入磁盘,运行您的 Web 应用程序的应用程序池标识将需要写入权限。您需要设置应用程序池,您需要选择 IIS AppPool\YourApplicationPool,其中 YourApplicationPool 是您新创建的应用程序池,而不是 NT Authority\Network Service。您可以在herehere 找到更多相关信息。

【讨论】:

    【解决方案2】:

    以下示例通过将 Archive 和 Hidden 属性应用于文件来演示 GetAttributes 和 SetAttributes 方法。

    来自http://msdn.microsoft.com/en-us/library/system.io.file.setattributes.aspx

     using System;
     using System.IO;
     using System.Text;
    
    class Test 
    {
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
    
        // Create the file if it exists. 
        if (!File.Exists(path)) 
        {
            File.Create(path);
        }
    
        FileAttributes attributes = File.GetAttributes(path);
    
        if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
        {
            // Show the file.
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
            File.SetAttributes(path, attributes);
            Console.WriteLine("The {0} file is no longer hidden.", path);
        } 
        else 
        {
            // Hide the file.
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
            Console.WriteLine("The {0} file is now hidden.", path);
        }
    }
    
    private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
    {
        return attributes & ~attributesToRemove;
    }
    }
    

    如果您需要更多帮助,请告诉我。

    编辑

    还要确保网络服务和 IUSR 可以访问网络应用程序。

    【讨论】:

    • 这与OP关于如何删除只读属性的问题有关?我不认为这是他们不知道如何删除只读属性的情况 - 这是它适用于一个应用程序而不是另一个应用程序的情况 - 正如其他人在 cmets 中所说的那样,这很可能是一个权利问题。
    猜你喜欢
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    相关资源
    最近更新 更多