Solved

Make file read-only

Posted on 2006-11-09
6
225 Views
Last Modified: 2010-04-16
Hi i am downloading an excel file in C# and i am using the following:

System.IO.FileStream fs = new System.IO.FileStream("C:\\" + fname, System.IO.FileMode.OpenOrCreate);

Is there a way for me to make the file readonly?
0
Comment
Question by:joan2006
  • 2
  • 2
6 Comments
 
LVL 8

Assisted Solution

by:redpipe
redpipe earned 250 total points
ID: 17906478
Opening a readonly stream:
FileInfo fi = new FileInfo("c:\myfile.txt");
FileStream s = fi.Open(FileMode.OpenOrCreate, FileAccess.Read);

Making a file readonly:
FileInfo fi = new FileInfo(FileName);
fi.IsReadOnly = true;
0
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 17907102
You can try this:

File.SetAttributes("C:\\" + fname, FileAttributes.ReadOnly);


Hope it helps.
Regards,
Mishu
0
 

Author Comment

by:joan2006
ID: 17908088
OK I cant get this to work.   Here is the full view of the code...  Can someone help me determin what needs to be done with the code i have to make the file read only......THanks!

string fname = (string)dr[0];

System.IO.FileMode.OpenOrCreate);
            System.IO.FileStream fs = new System.IO.FileStream("C:\\" + fname, System.IO.FileMode.OpenOrCreate);
            fs.Write((byte[])dr[1], 0, ((byte[])dr[1]).Length);
           
            fs.Flush();
            fs.Close();
            connection.Close();
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("Content-Disposition", "attach;filename=" + fname);
            Response.TransmitFile("C:\\" + fname);
            Response.End();
0
 
LVL 11

Accepted Solution

by:
Mihai Stancescu earned 250 total points
ID: 17908185
After you close the file stream set this:
File.SetAttributes("C:\\" + fname, FileAttributes.ReadOnly);


Regards,
Mishu
0
 
LVL 8

Expert Comment

by:redpipe
ID: 18084206
I see multiple plausible solutions to his problem and since the asker has not made any comment on errors/problems a point-split should be in place
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question