[英]open pdf file selected in listbox
I have all the dirctories (2014, 2012), the files of each selected folder (.pdf) in the listbox 2 我有所有目录(2014、2012),以及列表框2中每个选定文件夹的文件(.pdf)。
I get the dirctories by this code 我通过此代码获取目录
if (FBD.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
DirectoryInfo[] diri_info = newDirectoryInfo(FBD.SelectedPath).GetDirectories();
foreach (DirectoryInfo diri in diri_info)
{
listBox1.Items.Add(diri);
}
and i get the files by this code 我通过此代码获取文件
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
{
DirectoryInfo dirictory_choisis = (DirectoryInfo)listBox1.SelectedItem;
FileInfo[] files = dirictory_choisis.GetFiles();
listBox2.Items.Clear();
foreach (FileInfo file in files)
{
listBox2.Items.Add(file);
}
}
else
{
MessageBox.Show("selectioner un dossier");
}
}
Now how I can open the selected file (.pdf) ? 现在如何打开所选文件(.pdf)?
i use this code but dosn't work ( throw an exception file dosn't found) 我使用此代码,但不起作用(抛出未找到的异常文件)
private void listBox2_Click(object sender, EventArgs e)
{
FileInfo file = (FileInfo) listBox2.SelectedItem;
Process.Start(file.Name);
}
There is a syntax error in your code: "newDirectoryInfo" 您的代码中存在语法错误:“ newDirectoryInfo”
By the way, file.Name only returns the name (not including path). 顺便说一句,file.Name仅返回名称(不包括路径)。 You should replace that line with:
您应该将该行替换为:
Process.Start(file.FullName);
So the listBox2_Click should be like this: 所以listBox2_Click应该像这样:
private void listBox2_Click(object sender, EventArgs e)
{
FileInfo file = (FileInfo)listBox2.SelectedItem;
Process.Start(file.FullName);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.