编程实现双击某个文件用指定程序打开

資料收集如下:

http://topic.csdn.net/u/20090515/11/df6dd966-3abf-459c-a4d2-d5a0fc60c264.html

http://www.cnblogs.com/barrysgy/archive/2011/10/31/2230005.html

http://www.cnblogs.com/yao/archive/2006/06/26/436038.html

 

http://www.baidu.com/s?wd=C%23+ShellExecute&rsv_bp=0&rsv_spt=3&n=2&inputT=4875

http://www.baidu.com/s?wd=%B1%E0%B3%CC%CA%B5%CF%D6%CB%AB%BB%F7%C4%B3%B8%F6%CE%C4%BC%FE%D3%C3%D6%B8%B6%A8%B3%CC%D0%F2%B4%F2%BF%AA+&rsv_bp=0&rsv_spt=3&n=2&inputT=546

 

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace OpenFile
{
public partial class MainForm : Form
{

public MainForm()
{
InitializeComponent();
}

public enum ShowCommands : int
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_FORCEMINIMIZE = 11,
SW_MAX = 11
}
[DllImport("shell32.dll")]
static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
ShowCommands nShowCmd);


private void btnSelect_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog=new OpenFileDialog();
openFileDialog.InitialDirectory="c:\\";//注意这里写路径时要用c:\\而不是c:\
openFileDialog.Filter="文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFileDialog.RestoreDirectory=true;
openFileDialog.FilterIndex=1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
this.tBFile.Text = openFileDialog.FileName;
}
}

private void btnOpen_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.tBFile.Text))
{
String filepath;
filepath = this.tBFile.Text;
//ShellExecute(IntPtr.Zero, "open", filepath, "", "", ShowCommands.SW_SHOWNORMAL);
System.Diagnostics.Process.Start(filepath);

}
else
{
MessageBox.Show("Please Select File");
}
}
}
}

 

posted @ 2011-11-29 14:01  Athrun  阅读(598)  评论(0编辑  收藏  举报