我是C#的新手,但我正在做一个小小的申请。
您可以添加应用程序(使用OpenFileDialog)。它将添加的应用程序链接到一个按钮(提取文件图标和exe名称)。然后,当单击按钮时,它将启动所添加的应用程序。
您可以添加多达5个应用程序(5个按钮),这一切都可以。但是,如果添加超过1个应用程序,它只打开最后一个添加的应用程序。
因此,如果我添加了Word / Excel / Outlook,它只会打开所有按钮上的Outlook。
我的问题是:
我如何使它,使它记住什么应用程序打开时,按钮被点击。
应用程序屏幕截图,这样您就可以看到:
http://s24.postimg.org/467l561dh/Untitled.png
码
public partial class QuickStarter : Form
{
public QuickStarter()
{
InitializeComponent();
}
Icon ico = null;
OpenFileDialog ofd = new OpenFileDialog();
private void application1ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button1.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button1.Image = ico.ToBitmap();
button1.Enabled = true;
}
}
private void application2ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button2.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button2.Image = ico.ToBitmap();
button2.Enabled = true;
}
}
private void application3ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button3.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button3.Image = ico.ToBitmap();
button3.Enabled = true;
}
}
private void application4ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button4.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button4.Image = ico.ToBitmap();
button4.Enabled = true;
}
}
private void application5ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button5.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button5.Image = ico.ToBitmap();
button5.Enabled = true;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @ofd.FileName;
Process.Start(start);
}
private void button2_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @ofd.FileName;
Process.Start(start);
}
private void button3_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @ofd.FileName;
Process.Start(start);
}
private void button4_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @ofd.FileName;
Process.Start(start);
}
private void button5_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @ofd.FileName;
Process.Start(start);
}
private void resetApplicationsToolStripMenuItem_Click(object sender, EventArgs e)
{
button1.Text = "Application";
button1.Enabled = false;
button1.Image = null;
button2.Text = "Application";
button2.Enabled = false;
button2.Image = null;
button3.Text = "Application";
button3.Enabled = false;
button3.Image = null;
button4.Text = "Application";
button4.Enabled = false;
button4.Image = null;
button5.Text = "Application";
button5.Enabled = false;
button5.Image = null;
}
}发布于 2014-12-10 18:10:03
您需要在某个地方实际存储文件名数据,以完成您想要做的事情。目前,您正在对所选的每个应用程序重用OpenFileDialog实例,以便只保存最后选择的文件的信息。
您可以添加一个私有字段来存储此信息。如果你只有5个按钮,一个简单的数组就能工作了。
public partial class QuickStarter : Form
{
public QuickStarter()
{
InitializeComponent();
}
Icon ico = null;
OpenFileDialog ofd = new OpenFileDialog();
// Added
string[] fileNames = new string[5];现在,在您的处理程序中添加一个按钮,您可以添加行
private void application1ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button1.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button1.Image = ico.ToBitmap();
button1.Enabled = true;
// Added Line
fileNames[0] = ofd.FileName;
}
}现在,在单击按钮处理程序中,不是从OpenFileDialog中提取名称,而是从数组中提取名称。
private void button1_Click(object sender, System.EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = fileNames[0];
Process.Start(start);
}现在您将打开正确的应用程序。只需确保将数组的索引更改为每个处理程序的正确编号。
但是,关于总体设计,您有很多重复的代码。您可能可以将所有这些代码简化为两个事件处理程序,根据object sender是谁来存储数据。这还允许您以后添加或删除按钮,而不必添加或删除其关联的事件处理程序。这也会增加一些额外的逻辑来存储信息。此时,List<string>或Dictionary<string, string>可能比基本数组更有用。
发布于 2014-12-10 18:11:43
您目前正在使用OpenFileDialogPath运行程序。这意味着您的按钮只适用于上次添加的程序
您要做的是,记住每个按钮的..exe路径如下:
Dictionary<Button,string> _exePaths = new Dictionary<Button,string>();
private void application1ToolStripMenuItem_Click(object sender, EventArgs e)
{
_userSelectExe(button1);
}
private void application2ToolStripMenuItem_Click(object sender, EventArgs e)
{
_userSelectExe(button2);
}
and so on_userSelectExe方法:
private void _userSelectExe(Button button)
{
using(OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
exePaths[button] = ofd.FileName;
button.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button.Image = Icon.ExtractAssociatedIcon(ofd.FileName).ToBitmap();
button.Enabled = true;
}
}
}然后像这样开始每一个:
// Assign this eventhandler to every button
private void _allButton_Click(object sender, EventArgs e)
{
_startButton((Button) sender);
}
private void _startButton(Button button)
{
string path;
if(_exePaths.TryGetValue(button, out path))
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = path;
Process.Start(start);
} else MessageBox.Show("No Exe for this button defined!");
}https://stackoverflow.com/questions/27407742
复制相似问题