get files with DirectoryInfo in Virtual directory

5.2k views Asked by At

I have a arrav string like this:

string[] imgList = new[] { "/Images/10000489Back20130827.jpg", "/Images/2F.jpg", "/Images/10000489Front20130827.jpg" };

that contain names of file, contained in an virtaul directory.

If this parameteres I assigned to an ImageUrl, the image is displayed. In the detail of the pages show the propertie like this:

src="/Images/1F.jpg"

But when I try to looking for the files in specific directory all the files and assigned to an ImageUrl rpoperties, the images it's not displayed. I note that the path retrieve complete, and not a reference of the virtaul directory

string path = "/Images"; ///Obtener el path virtual

DirectoryInfo directory = new DirectoryInfo(path);
FileInfo[] files = directory.GetFiles("*.jpg");

imgList = files.Where(x => x.FullName.Contains(clientNumber)).Select(x => x.FullName).ToList().ToArray();

I retrieve this path:

src="C:/Images/1F.jpg"

How can I get only the virtual path with the name of the file using DirectoryInfo class?

3

There are 3 answers

0
iceheaven31 On

try this:

string path = "/Images";
DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath(path));
0
Khristian Liahut On

I resolved it:

Instead to pass the full path:

x.FullName

I concatenate the virtual path with the file name.

path + x.Name

Example

imgList = files.Where(x => x.FullName.Contains(clientNumber)).Select(x => path + x.Name).ToList().ToArray();
0
soundar rajan On

Show Image In Virtual Path Like (C:\Users\User\Desktop\Signature.png) : Its Working

I used Try Catch To Avoid Error

Cliend Side :

asp:Image runat="server" ID="Image1" />

Server Side :

try {

        Byte[] bytes = System.IO.File.ReadAllBytes(@"C:\Users\User\Desktop\Signature.png");
        Image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes, 0, bytes.Length);
        Image1.Visible = true;

    }

    catch (Exception e)
    {

    }