?
Solved

Form to display file contents?

Posted on 2014-01-03
15
Medium Priority
?
119 Views
Last Modified: 2015-03-10
VS 2008 C# 3.5

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 varible

2.

open a new form from a windows application

3.

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.
0
Comment
Question by:Michael Sterling
[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
  • 8
  • 7
15 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39754791
What are we talking about here? Text files? PDFs? Video files?
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 39754809
Sorry, yes just text files.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39754834
I think I'd just pass the forms application the path to the file, and let it open up the file itself, rather than pass it the text. For this purpose, you could do something like the following:

Main Application
string filename = "path\to\text\file.txt";

filename = "\"" + filename + "\"";  // Add quotes to ensure that paths containing spaces are sent correctly

System.Diagnostics.Process.Start(@"path\to\forms\application.exe", filename);

Open in new window


Forms Application
public class Form1 : Form
{
    protected void Form1_Load(object sender, EventArgs e)
    {
        string[] arguments = Environment.GetCommandLineArgs();
        string textFilename = arguments[1];  // Add error checking for IndexOutOfRange

        this.textBox1.Text = System.IO.File.ReadAllText(textFilename);
    }
}

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 1

Author Comment

by:Michael Sterling
ID: 39754885
@kaufmed: Thank you. How do I make the file read only?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39754888
The file itself, or the data on the form?
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 39754895
The file itself.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39754951
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.
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 39754965
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?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39754979
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;

Open in new window

0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 39755119
this is my code:

                        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;    

Open in new window


as it stands, i can still edit the file when it opens. what am i doing wrong?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39755202
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.
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 39755216
Oh, that's the thing, its actually opening up a notepad file (.txt) with content. there is no textbox.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39755272
My fault. I didn't look at your last code sample. Try moving the readonly code above the code that launches notepad.
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 39759802
@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");

Open in new window

0
 
LVL 1

Author Closing Comment

by:Michael Sterling
ID: 40657107
Thank you.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Six Sigma Control Plans

649 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