Michael Sterling
asked on
Form to display file contents?
VS 2008 C# 3.5
I need the quick and dirty version of how to:
are there any code examples of this that I can take a look at? i'm open to other options for performing this same task. i don't want the displayed content to be editable.
I need the quick and dirty version of how to:
1.
read the content of a file (or two files at the most) into a varible2.
open a new form from a windows application3.
display the contents of that file (those files) on that form.are there any code examples of this that I can take a look at? i'm open to other options for performing this same task. i don't want the displayed content to be editable.
What are we talking about here? Text files? PDFs? Video files?
ASKER
Sorry, yes just text files.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@kaufmed: Thank you. How do I make the file read only?
The file itself, or the data on the form?
ASKER
The file itself.
Well, your application won't be doing any writing to the file, so it is effectively read-only. If you need to explicitly have your code set this attribute on the file, well that is a different story, but can be achieved. Note, though, that setting this attribute really means nothing for your application unless your application is actually trying to write to the file.
ASKER
My application only needs to display the file. It won't be doing any writing to it other than for its intial creation. Matter of fact, I look for and delete the file (if it exits) before creating/writing to it again. It is truly only for displaying of content from other files that actually do get processed later on. How do I get my code to set this file's attribute to read only, before I open it?
If you set it, then you won't be able to delete the file. To answer your question:
System.IO.FileInfo yourFile = new System.IO.FileInfo(@"path\to\your\text\file.txt");
yourFile.IsReadOnly = true;
ASKER
this is my code:
as it stands, i can still edit the file when it opens. what am i doing wrong?
StreamWriter sw1 = new StreamWriter(@"FileContent.txt", true);
sw1.WriteLine(content);
sw1.Close();
System.Diagnostics.Process.Start("notepad.exe", @"FileContent.txt");
FileInfo myFile1 = new FileInfo(@"FileContent.txt");
myFile1.IsReadOnly = true;
as it stands, i can still edit the file when it opens. what am i doing wrong?
as it stands, i can still edit the file when it opens. what am i doing wrong?That depends on what you mean. If by "edit the file" you mean you can manipulate the text that you see on the form, well then I must tell you that you are not really editing the file. You are editing the text that is in the text box. To prevent such edits, change the ReadOnly property of the text box to True in the designer.
ASKER
Oh, that's the thing, its actually opening up a notepad file (.txt) with content. there is no textbox.
My fault. I didn't look at your last code sample. Try moving the readonly code above the code that launches notepad.
ASKER
@kaufmed: i tried this but to no avail.
FileInfo myFile1 = new FileInfo(@"FileContent.txt");
myFile1.IsReadOnly = true;
System.Diagnostics.Process.Start("notepad.exe", @"FileContent.txt");
ASKER
Thank you.