简体   繁体   English

在.ashx页面中使用文件的相对路径

[英]Using a relative path to a file in an .ashx page

Double apologies - first, I'm completely new to ASP.net so I'm sure that this is a very naive question, and second, it looks like there's already a hundred questions on SO about relative paths in ASP.NET. 双重道歉-首先,我是ASP.net的新手,所以我确定这是一个非常幼稚的问题,其次,SO上似乎已经有关于ASP.NET相对路径的一百个问题。 Unfortunately - I can't find one that answers my question. 不幸的是-我找不到能回答我问题的人。

Background - I'm working on an .ashx file that needs to be able to access an Excel file. 背景 - 我正在开发一个需要能够访问Excel文件的.ashx文件。

When I'm running locally and using an absolute path to the file, everything works great. 当我在本地运行并使用文件的绝对路径时,一切都很好。 Here's my code: 这是我的代码:

<%@ WebHandler Language="C#" Class="MyHandler" %>
using System;
... [a bunch more "usings..."]
using Microsoft.Office.Interop.Excel;

public class MyHandler : IHttpHandler {
    public void ProcessRequest(HttpContext context) {
        string path = @"C:\Users\[...]\myExcelFile.xls";
        Workbook theWorkbook;
        Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
        theWorkbook = app.Workbooks.Open(path);

However, I want to run this on a server, and use a relative path to the myExcelFile.xls . 但是,我想在服务器上运行它,并使用myExcelFile.xls的相对路径。

My project is set up like this: 我的项目设置如下:

Main folder:
 - Default.aspx
 - myExcelFile.xls
 - Web.config
 - Global.asax
Styles folder
Scripts folder
Handler folder
 - MyHandler.ashx (that's reading the Excel File)
bin folder

So - you can see that the Excel file is in the "root" of the application. 所以 - 您可以看到Excel文件位于应用程序的“根”中。

To make the path relative, I've tried the following with no success: 为了使路径相对,我尝试了以下但没有成功:

Assuming that "relative" is from myHandler.ashx: 假设“relative”来自myHandler.ashx:

string path = @"..\myExcelFile.xls":

Assuming that ~ refers to the "root": 假设~表示“根”:

string path = @"~\myExcelFile.xls";

Just guessing: 只是猜测:

string path = @".\myExcelFile.xls";

Just desperate: 只是拼命:

string path = "myExcelFile.xls";

None of these worked. 这些都不起作用。

So I did a little research here on SO, and found System.IO.Path.GetFullPath . 所以我在这里做了一些研究,发现了System.IO.Path.GetFullPath So I tried: 所以我尝试过:

string path = System.IO.Path.GetFullPath("myExcelFile.xls");

That didn't work, but at least I could see that the value of path was C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\DevServer\\11.0\\PinpointTool-DC.xls 那没有用,但是至少我可以看到path的值是C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\DevServer\\11.0\\PinpointTool-DC.xls

Of course - I don't understand that at all - that path is nowhere near where my project is (which means I don't understand what System.IO.Path.GetFullPath does...) 当然-我根本不明白-该路径离我的项目不远(这意味着我不了解System.IO.Path.GetFullPath作用...)

Last, I tried Server.MapPath("myExcelFile.xls") , but that doesn't work at all (locally, at least): 最后,我尝试了Server.MapPath("myExcelFile.xls") ,但这根本不起作用(至少在本地):

Compiler Error Message: CS0103: The name 'Server' does not exist in the current context

So, how can I get this to work (ideally both locally and on a server?) 那么,我怎样才能使它工作(理想情况下在本地和服务器上?)

While you could use context.Server.MapPath , You could also use a path that you define in your "web.config" file in the appSettings section and read it later in your application: 虽然可以使用context.Server.MapPath ,但是也可以使用在appSettings部分的“ web.config”文件中定义的路径,并在以后的应用程序中阅读它:

Web.config: Web.config文件:

...
<appSettings>
    <add key="myBasePath" value="C:\Some\Path" />
</appSettings>
...

In your ASHX handler you could write something like: 在您的ASHX处理程序中,您可以编写如下内容:

...
string path = Path.Combine(
    ConfigurationManager.AppSettings[@"myBasePath"],
    @"myExcelFile.xls");
...

You can (and should) have different "web.config" files for your development machine and your production environment. 您可以(并且应该)为您的开发机器和生产环境使用不同的“ web.config”文件。

To second John Saunders' remark regarding Office Interop on the server , I strongly recommend to use a decidated library like 对于约翰·桑德斯(John Saunders)关于服务器上的Office Interop 的评论 ,我强烈建议使用决定性的库,例如

I worked successfully with both of them (commerical, though). 我和他们两个都成功地工作了(尽管是商业的)。

have you tried 你有没有尝试过

System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/."),"myExcelFile.xls");

Hope this will help 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM