create uri to file in current working directory

Posted on 2014-08-12
Last Modified: 2016-06-10
I’m trying to write a c# program using EPPlus ( that generates an Excel worksheet with thumbnails of images and links to the full-sized image.  The images reside in the same folder as the executable and the resulting spreadsheet. It is all working OK with the following:
int a = 1;
Image fullSize = Image.FromFile(@"Screenshot.jpg");
Image.GetThumbnailImageAbort myCallback = new
Image thumb = fullSize.GetThumbnailImage(100, 71, myCallback, IntPtr.Zero);

var picture = worksheet.Drawings.AddPicture(a.ToString(), thumb);
picture.SetPosition(a-1 , 0, 6-1, 0);

worksheet.Cells[a, 6].Hyperlink = new Uri("file://c:/Screenshot.jpg");

Open in new window

What I want to do is make the whole thing location independent. As it is, the paths to the full-size jpgs are hard-coded. I would like them to be located wherever the current working folder is.

I have tried all sorts of things, and so far have been unable to get the .net Uri format to agree with Excel.

I did some digging and generated a sample spreadsheet in Excel and added a uri to a cell pointing to a jpg in the current folder. I then opened the .xls file with 7-zip (who knew you could do this?) and saw this snippet in sheet1.xml:
<hyperlinks><hyperlink ref="F2" r:id="rId1"/></hyperlinks>

Open in new window

In sheet1.xml.rels, I found this:
<Relationship Id="rId1" Type="" Target="Screenshot3.jpg" TargetMode="External"/>

Open in new window

Moving the xls file to the desktop and opening it with Excel and clicking the hyperlink produces a “cannot open the specified file” error. Moving the jpg file to the desktop and then clicking the hyperlink works fine.

So, it appears that Excel’s file format allows for hyperlinks to a relative uri. The question is, how do I generate such a uri and hyperlink programmatically?

I'm stuck at something that really ought to be easy. The hacker in me says I should just script up something that disassembles the .xlsx file (I had no idea until today that you could do that with 7-zip), make the necessary changes and then zip it back up. But that's absolutely nuts, I think.

There's GOT to be a better way,
Question by:1_21gigawatts
    LVL 24

    Expert Comment


    Author Comment

    causes this error:
      Invalid URI: A Dos path must be rooted, for example, 'c:\'.
    LVL 24

    Accepted Solution



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Viewers will learn a basic data manipulation technique of unpivoting data in Power Query for Excel 2013 and the importance of using good data. Start with data in a poor structure: Create a table on your data: Unpivot columns: Rename columns: …
    Video by: Zack
    Viewers will learn the basics of using Excel Tables, the benefits found with them, and some pitfalls.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now