If you find the code below useful, leave a comment. Thanks.
/*
2007-03-19 John Goodwin
Wrote following code as example how to use C# script to show a dialog for a choice of files.
You can get C# script from http://www.csscript.net/
You can also add this file to a C# project, and compile it normally.
*/
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
class Script
{
[STAThread]
static public void Main(string[] args)
{
string path = @"C:\WINNT\";
string filemask = "*.log";
string selectedFile = GetMenuChoice(path, filemask);
if (selectedFile != string.Empty)
{
MessageBox.Show("Selected : " + selectedFile);
} else
{
MessageBox.Show("No selection");
}
}
private static string GetMenuChoice(string path, string filemask)
{
ChoiceForm frmMain = new ChoiceForm();
// load files
DirectoryInfo diFolder = new DirectoryInfo(path);
foreach (FileInfo file in diFolder.GetFiles(filemask))
{
frmMain.lbFiles.Items.Add(file.Name);
}
if (frmMain.ShowDialog() == DialogResult.OK)
{
return Convert.ToString(frmMain.lbFiles.SelectedItem);
} else
{
return string.Empty;
}
}
private class ChoiceForm : Form
{
private ListBox _lbFiles;
private Button _btnOK;
private Button _btnCancel;
public ChoiceForm()
{
Size = new Size(300,300);
StartPosition = FormStartPosition.CenterParent;
_lbFiles = new ListBox();
Controls.Add(lbFiles);
lbFiles.Size = new Size(294,250);
lbFiles.Anchor = (AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom);
_btnOK = new Button();
Controls.Add(btnOK);
btnOK.Location = new Point(4,248);
btnOK.DialogResult = DialogResult.OK;
btnOK.Anchor = (AnchorStyles.Left | AnchorStyles.Bottom);
btnOK.Text = "OK";
btnCancel = new Button();
Controls.Add(btnCancel);
btnCancel.Location = new Point(80,248);
btnCancel.DialogResult = DialogResult.Cancel;
btnCancel.Anchor = (AnchorStyles.Left | AnchorStyles.Bottom);
btnCancel.Text = "Cancel";
}
public ListBox lbFiles { get { return _lbFiles; } set { _lbFiles = value; } }
public Button btnOK { get { return _btnOK; } set { _btnOK = value; } }
public Button btnCancel { get { return _btnCancel; } set { _btnCancel = value; } }
}
}