In a C# 2008 application, the code listed below ends up in the catch exception block. The error message says the file can not be
located in the specified location: "C://temp//09-24-2012//companyname_company.xlsx".
The value for the String fileName in the createCust is obtained from the value passed
to this method. The file does exist on the specified location.
The code I am referring to is listed below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Logging;
using System.IO;
namespace sample1
{
public class Helper1
{
private static ILog log = LogManager.GetCurrentClassLogger();
public Customer createCust(String CustId, String fileName)
{
Customer cst = new Customer();
byte[] buffer = null;
try
{
System.IO.FileStream fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fileStream);
long totalBytes = new System.IO.FileInfo(fileName).Length;
buffer = binaryReader.ReadBytes((Int32)totalBytes);
DateTime attachmentId = DateTime.Now;
cst.Id = attachmentId.ToString();
cst.MimeType = getMimeType(fileName);
cst.Value = buffer;
fileStream.Close();
binaryReader.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return cst;
}
}}
I do not have any idea of why the file can not be located. The file exists in the path
that is passed to the createCust method. The location of the file path is specified as,
"C://temp//09-24-2012//companyname_company.xlsx".
Thus can you tell me what could be causing the file not to be located and how to fix the problem?
Loading
VulpesPosted Sep 25, 2012, 5:39 AM
One possibility is that the forward slashes shouldn't be doubled, the path should just be:
"C:/temp//09-24-2012/companyname_company.xlsx"
I'd also make sure that there's no leading whitespace in the path name by calling the TrimStart() method:
fileName = fileName.TrimStart();