I have list box that contains paths to specific files in the directory that the code reads and parses data and do ohter stuff with it. The error I get is Unable to cast object of type 'System.String' to type 'System.Web.UI.HtmlControls.HtmlInputFile' and I'm not sure how to overcome this.
I have a html INPUT control of type file whose only function is to get the directory path becuase the directory is not always the same. Then I split the PostedFile.Filename which looks like this C:\temp\2013\03-2013\Calib 100 for 29 Mar 13\211jd13100.txton '\' into an string array. The I reasseble path in a string by adding the array elements less the last index and use that as param for 'Directory.GetFiles(string) to get all my files in the directory. Again, I don't know of anohter way to get the directory information. Anyway I'll just post the code, it will be easier to understand.
static public ArrayList hif = new ArrayList();
static string[] filePaths;
protected void btnAddFile_Click(object sender, System.EventArgs e)
{
if (Page.IsPostBack == true)
{
StringBuilder sb = new StringBuilder();
string[] dirLocation = fileUpload.PostedFile.FileName.Split('\\');
for (int i = 0; i < dirLocation.Length - 1; i++)
{
sb.Append(dirLocation[i].ToString() + "\\");
}
// The assenbled directory path is being used to get the files.
filePaths = Directory.GetFiles(sb.ToString());
for (int i = 0; i < filePaths.Length; i++)
{
hif.Add(filePaths[i]);
lbxSelectedFiles.Items.Add(filePaths[i]);
}
}
}
The method above loads the listbox and filePath array with file path and name. The method below takes care of parsing but before that takes place I need to get the files and the error happens the foreach statement between the parents ().
foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
{
try
{
File = HIF.PostedFile;
StreamReader data = new StreamReader(HIF.PostedFile.InputStream);
PathFilename = File.FileName.ToString();
DirectoryInfo directory = new DirectoryInfo(PathFilename);
Directory = directory.Parent.ToString();
.
.
.
}
}
HtmlInputFileHtmlInputFilecontrol?