Make file read-only

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?
joan2006Asked:
Who is Participating?
 
Mihai StancescuConnect With a Mentor Software Engineer Commented:
After you close the file stream set this:
File.SetAttributes("C:\\" + fname, FileAttributes.ReadOnly);


Regards,
Mishu
0
 
redpipeConnect With a Mentor Commented:
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
 
Mihai StancescuSoftware Engineer Commented:
You can try this:

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


Hope it helps.
Regards,
Mishu
0
 
joan2006Author Commented:
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
 
redpipeCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.