打开特定的文本文件

时间:2012-04-21 03:44:12

标签: c#

我正在尝试在我的驱动器上打开一个特定文本文件的文本文件,我不想包含文本框或查看/浏览要打开的文件。我只是想让它在没有通过文本框的情况下自动打开磁盘驱动器上指定的文本文件,单击按钮后,驱动器中的特定文本文件应自动打开

如下所示:

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

每当我点击按钮时,我希望它打开此处指定的文件。

4 个答案:

答案 0 :(得分:3)

如果您想使用记事本或任何其他默认程序打开txt文件,您可以这样做:

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

如果要阅读代码中的文件,可以执行以下操作:

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

您还可以使用以下代码阅读文件内容:

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

我不确定那是不是你的意思,但是......

答案 3 :(得分:1)

也许像

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;
  }
}