user2477281
user2477281

Reputation: 1

Remove read only attribute from file while working on web application

How to remove read only attribute from file while working on web application in c#. Same I could do this on windows application by having this code.

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

The same code I tried in web application, it is not removed the read only property. Please help me to resolve on same.

Upvotes: 0

Views: 290

Answers (2)

lex87
lex87

Reputation: 1326

The application pool identity running your web application will require write access if your application writes to disk. You need to set up you application pool, you need to choose IIS AppPool\YourApplicationPool where YourApplicationPool is your newly created app pool instead of NT Authority\Network Service. You can find more about it here and here.

Upvotes: 1

Rajeev Bera
Rajeev Bera

Reputation: 2019

The following example demonstrates the GetAttributes and SetAttributes methods by applying the Archive and Hidden attributes to a file.

From 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;
}
}

Let me know if you need more help.

EDIT

Also make sure Network service and IUSR has access to the web app.

Upvotes: 0

Related Questions