Title: How to open a text file in read-only mode means there i can not perform write?
Tags: c#;asp.net;.net
Question: I am trying it too much time but can not achieve the goal. File is opened but its opened in write mode.
in txtpath.text, I am passing the path of the text:
System.IO.FileInfo fileObj= new System.IO.FileInfo(txtPath.Text);
fileObj.Attributes = System.IO.FileAttributes.ReadOnly;
System.Diagnostics.Process.Start(fileObj.FullName);
Comment: I'm with Yahia here... something with this seems very wrong. You aren't "reading" the file at all, you are telling window to "execute" the file, and it will then try to open it in the default editor. But that makes no sense in an ASP.NET app.

Comment: why are you using `Process.Start` for opening a file in an ASP.NET application?


Here is another answer: Opening a file to only read it's contents:
// Open the stream and read it back.
using (FileStream fs = File.OpenRead(path))
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) &gt; 0)
{
Console.WriteLine(temp.GetString(b));
}
More about File.OpenRead: http://msdn.microsoft.com/en-us/library/system.io.file.openread.aspx
Setting a file's ReadOnly attribute and executing it:
File.SetAttributes(txtPath.Text, File.GetAttributes(txtPath.Text) | FileAttributes.ReadOnly);
System.Diagnostics.Process.Start(txtPath.Text);

Here is another answer: use the File.OpenRead method
string sFilename = "myfile.txt";
FileStream SR = File.OpenRead(sFilename);

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:02/08/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><asp.net><.net>', 'Title': 'How to open a text file in read-only mode means there i can not perform write?', 'ViewCount': 6093, 'id': 9189374, 'user_AboutMe': "<p>I am Ashish Verma.I have done MCA in 2010.\nCurrently,i am looking for job.I have total 1.6 year exp on Asp.net with C#\nand Sql Server 2005/08.</p>\n\n<p>I am building my logics and Concept and Clearify some code confusion tat's y \ni am creating my profile on StackOverflow.\nso, I hope that some developers will help me.</p>\n\n<p>Thanks-\nAshish</p>\n", 'user_AccountId': 1226137.0, 'user_DisplayName': 'Ashish Verma', 'user_Reputation': 127.0, 'user_Views': 59.0, 'user_id': 1191628.0}}