kryticrecte
kryticrecte

Reputation: 377

Opening a specific text file

I am trying to open a text file one specific text file on my drive and I do not want to include the textbox or look/browse for a file to open. I just want it to open the text file specified on the disk drive only automatically without passing through the textbox, upon clicking a button a specific text file in the drive should open automatically

Something like this below:

private void button1_Click(object sender, EventArgs e)
{
  c:\users\Dickson\Documents\oracle.txt;//
}

I want it to open this file specified here whenever I click the button.

Upvotes: 0

Views: 1582

Answers (4)

Jeromy Kelley
Jeromy Kelley

Reputation: 31

Maybe something like

static class program
{
  static void Main()
  {
    System.Windows.Forms.Application.Run(new frmMain());
  }
}

public class frmMain : Form
{
  private System.Windows.Forms.Label lblText;
  private System.Windows.Forms.Button btnLoadText;

  public frmMain()
  {
    InitializeComponent();
  }

  private void InitializeComponent()
  {
    this.lblText = new System.Windows.Forms.Label();
    this.lblText.AutoSize = true;
    this.lblText.Dock = System.Windows.Forms.DockStyle.Top;
    this.lblText.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.lblText.Location = new System.Drawing.Point(0, 0);
    this.lblText.Name = "lblText";
    this.lblText.Size = new System.Drawing.Size(170, 24);
    this.lblText.TabIndex = 0;
    this.lblText.Text = "";
    this.Controls.Add(this.lblText);

    this.btnLoadText = new System.Windows.Forms.Button();
    this.btnLoadText.Location = new System.Drawing.Point(339, 24);
    this.btnLoadText.Name = "btnLoadText";
    this.btnLoadText.Size = new System.Drawing.Size(75, 23);
    this.btnLoadText.TabIndex = 29;
    this.btnLoadText.Text = "Load Text";
    this.btnLoadText.UseVisualStyleBackColor = true;
    this.btnLoadText.Dock = System.Windows.Forms.DockStyle.Bottom;
    this.Controls.Add(this.btnLoadText);
    this.btnLoadText.Click += new System.EventHandler(this.btnLoadText_Click);          
  }

  private void btnLoadText_Click(object sender, EventArgs e)
  {
    StreamReader streamReader = new StreamReader("test.txt");
    string text = streamReader.ReadToEnd();
    streamReader.Close();
    this.lblText.Text = text;
  }
}

Upvotes: 1

Sid
Sid

Reputation: 1269

You can also read the contents of the file with the following code:

FILE *infile;
infile = fopen("file_name", "r");

Im not sure if thats what you meant, but..

Upvotes: 1

YoryeNathan
YoryeNathan

Reputation: 14532

If you want to open the txt file using notepad or any other default program, you can do:

Process.Start(@"C:\Users\Dickson\Documents\oracle.txt");

If you want to read the file in the code, you can do:

var txtContent = File.ReadAllText(@"C:\Users\Dickson\Documents\oracle.txt");

Upvotes: 3

Sadaf
Sadaf

Reputation: 602

System.Diagnostics.Process.Start(file path);

Upvotes: 1

Related Questions