如何開啟一個文件 在这个listbox的双剑事件里面写System.Diagnostics.Process.Start("C:\Program Files\Windows NT\Accessories\wordpad.exe","你的文件路径"); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 this.listBox1.SelectedItem.ToString();是你的文件名称但是你不要忘记加上路径 那我要是打開一個.bmp文件呢?我也希望能夠支持許多文件,難到我必須要知道這些文件是由哪些程序生成的。有沒有更簡單的方法? brightheroes(闭关|那一剑的风情) 说的方法我觉得以及很简单了事实上,Process.Start会根据你提供的filename自动去找到文件关联,找到需要打开该文件的程序,txt也好,bmp也好,如果你非要用指定的程序打开,那就要在start方法中指定程序路径。我觉得楼主有一个问题忽略了,你要的是双击listbox中每一项,实际上listbox并没有提供这个事件,你该怎么办?:) 不过还真没有注意过System.Diagnostics.Process.Start的重载3方法:) 按楼主和 brightheroes(闭关|那一剑的风情)思路写的,双击选中项就启动程序namespace lei198203{ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Diagnostics; /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ListBox listBox1; private Point mousePnt=Point.Empty; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.listBox1 = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // listBox1 // this.listBox1.ItemHeight = 12; this.listBox1.Items.AddRange(new object[] { "d:\\lei198203.txt ", "d:\\U738P28T3D506436F328DT20040916140536.jpg"}); this.listBox1.Location = new System.Drawing.Point(0, 8); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(288, 184); this.listBox1.TabIndex = 0; this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick); this.listBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseUp); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.listBox1}); this.MaximizeBox = false; this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void listBox1_DoubleClick(object sender, System.EventArgs e) { string path=listBox1.SelectedItem as string; if(path==null||path.Length==0) return; int index=listBox1.SelectedIndex; int height=listBox1.ItemHeight; if(height*index<mousePnt.Y&&height*(index+1)>mousePnt.Y) Process.Start(path); } private void listBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { this.mousePnt=new Point(e.X,e.Y); } }} 非常感謝大家的支持,感謝 brightheroes(闭关|那一剑的风情) 和 micropentium6(小笨|想学ASP) 提供的代碼。 C#离线版 帮助文档那里弄 将参数值从 SqlParameter 转换到 String 失败 C# 窗体 请教将半结构化数据转成XML数据 请问,如何把这些数据读到数组中。注,数据间是制表符非空格 问两个WinForm下的DataGrid的问题 c#控制台程序怎么oracle连接数据库 求助winform下载功能 如何判断一个url是pdf文件或word,ppt等非html文件 如何使用C#语言作为脚本? 老问题:请教打包程序中.NET框架的安装 richtext控件如何实现在输入的字符是指定的某一类型的字符串显示不同颜色
但是你不要忘记加上路径
事实上,Process.Start会根据你提供的filename自动去找到文件关联,找到需要打开该文件的程序,txt也好,bmp也好,如果你非要用指定的程序打开,那就要在start方法中指定程序路径。我觉得楼主有一个问题忽略了,你要的是双击listbox中每一项,实际上listbox并没有提供这个事件,你该怎么办?:)
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private Point mousePnt=Point.Empty;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Items.AddRange(new object[] {
"d:\\lei198203.txt ",
"d:\\U738P28T3D506436F328DT20040916140536.jpg"});
this.listBox1.Location = new System.Drawing.Point(0, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(288, 184);
this.listBox1.TabIndex = 0;
this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
this.listBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseUp);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listBox1});
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void listBox1_DoubleClick(object sender, System.EventArgs e)
{
string path=listBox1.SelectedItem as string;
if(path==null||path.Length==0)
return;
int index=listBox1.SelectedIndex;
int height=listBox1.ItemHeight;
if(height*index<mousePnt.Y&&height*(index+1)>mousePnt.Y)
Process.Start(path);
} private void listBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.mousePnt=new Point(e.X,e.Y);
}
}
}