开发者

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

First I'll explain the project structure:

  • Solution
    • Templates
      • Sport
        • Sport.doc
  • Opendoc.aspx
    • Opendoc.aspx.cs

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.


Try

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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜