So what you need to do is compare the selected item from the listbox or all the items in the list box (up to you), to the GetFiles call.
If there is a match print the file.
Also Explained further
here[
^]
private void btnPrint_Click(object sender, EventArgs e) {
string dir = @"C:\slimeyyyy";
if (Directory.Exists(dir)) {
string[] files = Directory.GetFiles(dir);
if (files.Length > 0) {
foreach (string file in files) {
string fileName = Path.GetFileName(file);
foreach (object item in listBox1.Items) {
if (fileName == item.ToString()) {
PrintDocument(Path.GetFullPath(file));
}
}
}
for (int i = 0; i < listBox1.Items.Count; i++) {
foreach (string file in files) {
string fileName = Path.GetFileName(file);
if (fileName == listBox1.Items[i].ToString()) {
PrintDocument(Path.GetFullPath(file));
}
}
}
}
}
}
private void PrintDocument(string path) {
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = path;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (!p.CloseMainWindow()) {
p.Kill();
}
}