1

I want to read a file in my e: drive from asp.net mvc application. When I try to access it from FileStream Class , file not found exception is thrown. Here is the code.

 public byte[] GetEncFile(string path)
    {
        FileInfo fInfo = new FileInfo(path);
        FileStream encFileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        BinaryReader reader = new BinaryReader(encFileStream);

        byte[] encFileBytes = reader.ReadBytes((int)fInfo.Length);

        return encFileBytes;
    }
9
  • What line is the exception thrown from? Is there any other information in the error? Commented Jul 4, 2014 at 5:34
  • make sure to give right path of file.. Commented Jul 4, 2014 at 5:34
  • on second line "FileStream encFileStream = new FileStream(path, FileMode.Open, FileAccess.Read);" Commented Jul 4, 2014 at 5:35
  • i can access my file on c: drive but not from e: , d:, drives Commented Jul 4, 2014 at 5:36
  • 2
    On your e: drive being the client or the server? Commented Jul 4, 2014 at 5:36

1 Answer 1

1

If Drive exists, then you should use

HttpContext.Current.Server.MapPath

Because you might be using virtual path in mathod paramter.

try :

FileInfo fInfo = new FileInfo(Server.MapPath(path));

will convert virtual path to physical path.

Hope will help.

Sign up to request clarification or add additional context in comments.

1 Comment

That Server.MapPath converts a site-relative path to a full physical path. If the OP already knows the full path ("on my E: drive"), then that wouldn't help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.