编程实现双击某个文件用指定程序打开
資料收集如下:
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
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");
}
}
}
}
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。