Liquid
Liquid

Reputation: 1871

Opening a Word document with a relative path from c# in asp.net

First I'll explain the project structure:

Now the problem. In the Opendoc.aspx.cs I'm trying to open Sport.doc but that isn't working when i'm using a relative path. only when I hard code it like c://Sport.doc.

After I click on a button on that page I go to this method:

protected void btnCreateWordBulletin_Click(object sender, EventArgs e)
{
    string path = VirtualPathUtility.ToAbsolute("~/Sport/Sport.doc");
    string Savepath = VirtualPathUtility.ToAbsolute("~/Sport/SportEvent.doc");
    CreateWordDocument(path, Savepath );
}

protected void CreateWordDocument(object fileName, object saveAs)
{
    //Set Missing Value parameter - used to represent
    //a missing value when calling methods through interop
    object missing = System.Reflection.Missing.Value;

    //Setup the Word.App class
    Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
    Microsoft.Office.Interop.Word.Document aDoc = null;
    // Check to see that file exists
    if (System.IO.File.Exists((string)fileName))
    {... Activating doc etc...}}

But the System.IO.File.exists doesn't seem to find the document.

I tried using .//Sport/sport.doc , ../Sport.doc, .//Template/Sport/sport.doc, ..//Template/Sport.doc.

Upvotes: 2

Views: 5328

Answers (2)

Dennis Röttger
Dennis Röttger

Reputation: 1985

Try

HttpApplication.Server.MapPath("/Solution/Templates/Sport/Sport.doc");

Thanks! Also please comment if it doesn't work.

Upvotes: 2

anothershrubery
anothershrubery

Reputation: 21003

Surely it would be ../Solution/Templates/Sport/Sport.doc?

EDIT: Actually, It would just be /Solution/Templates/Sport/Sport.doc If the aspx file is in the same folder as the Solution folder.

Bottom line is, you need to refernce the Solution folder.

Upvotes: 0

Related Questions