Solved

Make file read-only

Posted on 2006-11-09
6
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

688 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