Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
C#
namespace DirList1
{
    public partial class Form1 : Form
    {
        const string rootFolder = @"C:\MyDoc";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            {
                DirectoryInfo dinfo = new DirectoryInfo(rootFolder);
                FileInfo[] Files = dinfo.GetFiles("*.*");
                foreach (FileInfo file in Files)
                {
                    listBox1.Items.Add(file.Name);
                }
            }
        }
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

         Process.Start("notepad.exe", rootFolder + @"\" + (sender as ListBox).SelectedItem.ToString());
        }
        private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
        }
    }
}
Posted
Updated 14-Feb-14 17:23pm
v2
Comments
EduChapow 14-Feb-14 9:04am    
your click event is double click. try just a click.

any exception is throw?
izak_il 14-Feb-14 9:24am    
I only have text files no error message Just not open files
Member 10434230 14-Feb-14 9:29am    
Hi izak,
I checked at my end and it is opening text files properly. Can you send me the full path that you are passing to process.start
Member 10434230 14-Feb-14 9:06am    
What error are you getting?
phil.o 14-Feb-14 10:22am    
From famous philosopher Yoda: "Don't try ; do it, or not."
:)

Are you just having notepad compatible files? if you have multiple file types and you want OS to decide which application to use for opening the files, use below code:
Try to use:
Process.Start(rootFolder + @"\" + (sender as ListBox).SelectedItem.ToString());
 
Share this answer
 
make these changes, then it should work.

C#
// Another string to contain path and file.
string resultFilePath;
...
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    resultFilePath= rootFolder + @"\" + (sender as ListBox).SelectedItem.ToString();
}

private void button1_Click(object sender, EventArgs e)
{
    Process.Start("notepad.exe", resultFilePath);
}


Also make sure the control events are working. I have seen where someone will cut and paste event code but the event is not connected to the control.

You may also want to filter the file type to those of intrest, for instance if you only want to see text files use the following..
C#
FileInfo[] Files = dinfo.GetFiles("*.txt");
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900