What I have is a comboBox being populated from a code:
InitializeComponent();
DirectoryInfo dinfo = new DirectoryInfo(@"K:\ases");
FileInfo[] Files = dinfo.GetFiles("*.ssi", SearchOption.AllDirectories);
foreach (FileInfo file in Files)
{
comboBox1.Items.Add(file.Name);
}
Then i’m trying to ReadAllText from that selected item.
string contents = File.ReadAllText(comboBox1.Text);
When executed, it tries to read the file from the locale path, and of course the file isnt there. I also can’t set the working directory because the comoboBox is populated from a huge range of subdirectories. How do I get the working directory of the item selected in the combobox WITHOUT expossing the whole directory path to the user?
Any help always welcomed
I thought i saw a few answers with someone suggesting private and hiding things in a combox box, where those suggestions taken down. is there a way to keep the full file info in the combobox and only display the file name?
There are several different ways to accomplish this, but the absolute easiest would be to store the directory path in an instance variable (we’ll call it
directoryPath) and useSystem.IO.Path.Combine()to reconstruct the path:(some code eliminated for brevity)
You could also try adding the
FileInfoto theComboBoxrather than the file’s name and useSelectedItemrather thanText:Then you can do this to retrieve the file: