c#下载按钮url错误

c#下载按钮url错误,c#,.net,winforms,C#,.net,Winforms,下面是下载文件的代码。我的url有效(可以下载),但我的url2无效(无法下载)。我为url2所做的错误是什么?我的相对路径有问题吗 protected void Button1_Click(object sender, EventArgs e) { string url =@"C:\Users\Roy\Desktop\backup fyp\10-18-2011\WebSite5\123.txt"; string url2 = @"~\123.txt"; FileInfo

下面是下载文件的代码。我的url有效(可以下载),但我的url2无效(无法下载)。我为url2所做的错误是什么?我的相对路径有问题吗

protected void Button1_Click(object sender, EventArgs e)
{
    string url =@"C:\Users\Roy\Desktop\backup fyp\10-18-2011\WebSite5\123.txt";
    string url2 = @"~\123.txt";
    FileInfo finfo = new FileInfo(url);

    if (finfo.Exists)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + finfo.Name);
        Response.AddHeader("Content-Length", finfo.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.Flush();
        Response.WriteFile(finfo.FullName);
    }
    else
    {
        Response.Write("error");
    }
}

~\123.txt
是一个虚拟路径。您需要像这样使用它:

string url2 = HttpContext.Current.Server.MapPath(@"~\123.txt");

到底是什么不起作用?你有404错误吗?你拿错文件了吗?有什么事情发生吗?对于第一个url,我的文件可以下载,它是123.txt,对于url2,我的文件找不到=(