1

I want to get the file path from a folder/subfolder inside a folder and populate it to a image attributes. using HttpContext.Current.Server.MapPath(@"~\StorageFolders");will return full path and will return

Not allowed to load local resource

.

So now i am using looping and replacement method. Am i doing i right? or can i just get the full path excluding root folder example : the function returns

C:\WebsiteRootFolder\Myfolder\SubFolderA\handsome.png

to relative

Myfolder\SubFolderA\handsome.png

always return to the root folder of the website

 var fileList = Directory.GetFiles(path, "*", SearchOption.AllDirectories).ToList();
        var rootpath= HttpContext.Current.Server.MapPath("~");

foreach (var item in fileList)
        {
            string s = item.Replace(rootpath, "");
            Image image = new Image();
            image.ImageUrl = ResolveUrl(@"~\"+ s);
            //continue of codes
        }
MVC newbie
  • 579
  • 3
  • 9
  • 26

3 Answers3

0

Access Denied:

You don't have a right permission to that particular folder. Make sure you have to change the permission to full access to that folder.

Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
  • folder already full access. i reposted the error. the error is not because of permission. is how the website works which doesnt allow to access the file directly. – MVC newbie Sep 28 '17 at 01:52
0

Did you try using Path.GetFullPath method? https://msdn.microsoft.com/en-us/library/system.io.path.getfullpath(v=vs.110).aspx

farzaaaan
  • 410
  • 3
  • 9
0

instead of using

var fileList = Directory.GetFiles(path, "*", SearchOption.AllDirectories).ToList();

i changed to

  var fileList = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Select(x => (new FileInfo(x).FullName.Replace(root, ""))).ToArray();
MVC newbie
  • 579
  • 3
  • 9
  • 26