?
Solved

Make file read-only

Posted on 2006-11-09
6
Medium Priority
?
243 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 1000 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 1000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

770 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