Reputation: 63
When I try to run this I get the same error every time telling me that the file has not been found, even though it clearly is in that folder. Any ideas on how I can fix this?
Thank You.
public TaskViewer()
{
InitializeComponent();
DirectoryInfo dInfo = new DirectoryInfo(@"C:\\To-Do-List");
FileInfo[] Files = dInfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
listBox1.Items.Add(file.Name);
Path.GetDirectoryName(file.Name);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string file = listBox1.SelectedItem.ToString();
Process.Start(file);
}
Upvotes: 0
Views: 1326
Reputation: 24385
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string file = listBox1.SelectedItem.ToString();
string fullFileName = Path.Combine(@"C:\To-Do-List", file);
Process.Start(fullFileName);
}
Upvotes: 3
Reputation: 103437
You are putting file.Name
in the list-box, but that doesn't include the full path.
It looks like you're trying to do something about that with your Path.GetDirectoryName(file.Name);
, however, that function returns a string and you are just throwing it away.
Process.Start
needs the full path, or it will look in the current directory (probably your bin
folder).
So, the easy fix would be to use file.FullPath
instead of file.Path
.
That will cause the full path to appear in the list box, however. If you don't want that, the answer by DLeh works if all the files are in the same folder.
Upvotes: 2