Solved

Make file read-only

Posted on 2006-11-09
6
218 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
Comment Utility
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
Comment Utility
You can try this:

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


Hope it helps.
Regards,
Mishu
0
 

Author Comment

by:joan2006
Comment Utility
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
Comment Utility
After you close the file stream set this:
File.SetAttributes("C:\\" + fname, FileAttributes.ReadOnly);


Regards,
Mishu
0
 
LVL 8

Expert Comment

by:redpipe
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to read XML file attributes... 17 39
Using MS Code on my Mac 6 44
Long URL in SMS 6 22
VB.NET HttpWebRequest 12 30
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now