0

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.

2
  • Do you have the appropriate rights to this folder? Commented Jun 12, 2013 at 7:22
  • 1
    I'd guess the difference are the user rights: The Win App users is likely to have higher permissions than your Web App Pool user. Commented Jun 12, 2013 at 7:23

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

And this relates to OP's question about removing the Read-Only attribute how? I don't think it's a case of them not knowing how to remove the read-only attribute - it's a case of it works in one application and not in another - as others have said in the comments, it's most likely a rights issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.