2013-06-12 40 views
0

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

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

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

+0

您对此文件夹有适当的权利吗? –

+1

我猜想不同之处在于用户权限:Win App用户可能拥有比Web App Pool用户更高的权限。 – Filburt

回答

0

以下示例通过将“存档”和“隐藏”属性应用于文件来演示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访问Web应用程序。

+1

这与OP关于删除Read-Only属性的问题有关吗?我不认为这是他们不知道如何删除只读属性的情况 - 这是一个在一个应用程序中运行而不在另一个应用程序中的情况 - 正如其他人在评论中所说的那样,这很可能是一个供应问题。 – Tim

1

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

相关问题