如何使路径有“/”

时间:2012-03-28 06:20:17

标签: c# asp.net path filepath

我的打开新窗口弹出窗口有一些问题,它可以读取我的路径,就像扔掉我的“/”符号一样。所以它会看起来像这样的“C:UsersKHAIRADesktopheitechHibah Total v1.2 / Secure / PDF Folder”

任何人都可以帮我看看/读取这样的“C:Users / KHAIRA / Desktop / heitech / Hibah Total v1.2 / Secure / PDF Folder”。

我在gridview中打开了一个按钮,它将打开一个新窗口并在这里查看pdf文件,来自ViewDocument.aspx的编码

string commandName = e.CommandName.ToString().Trim();
GridViewRow row = GridView1.Rows[Convert.ToInt32(e.CommandArgument)];
string folderName = ConfigurationManager.AppSettings["folderPDF"].ToString();
string path = Server.MapPath("~") + "/Secure/";
string fullPath = path + folderName;
string[] filePaths = Directory.GetFiles(fullPath, "*.pdf"); 
switch (commandName)
{
    case "Open":                       
        string script = "<script language=\"JavaScript\">\n";
        script += "window.open ('OpenForm.aspx?path=" + row.Cells[0].Text;
        script += "','CustomPopUp', config='height=500,width=1024, toolbar=no, menubar=no, scrollbars=yes, resizable=yes,location=no, directories=no, status=no')\n";
        script += "</script>";

        this.ClientScript.RegisterStartupScript(this.GetType(), "onload", script);
        break;

用于OpenForm.aspx.cs编码:

catch(Exception ex)
{
    try
    {
        string paths = Request.QueryString["path"].ToString();
        bool fileExist = File.Exists(paths);
        if (fileExist)
        {
            Response.ContentType = "Application/pdf";
            Response.TransmitFile(paths);
        }
        else
        {
            Label1.Text = "File Not Exist";
        }
    }

但是,我意识到问题来自这里

string paths = Request.QueryString["path"].ToString();

3 个答案:

答案 0 :(得分:2)

首先要做的事情。

本地系统路径分隔符为\,例如C:\Windows

/适用于网络,例如http://stackoverflow.com/questions/9902129/how-to-make-the-path-have/9902194#9902194

对于单个\,您必须放置\\(记住转义序列)

使用字符串逐字

string path = @"C:\Users\KHAIRA\Desktop\heitech\Hibah Total v1.2\Secure\PDF Folder"

使用System.IO名称空间的Path.Combine方法,如

Path.Combine("C:", "Users");

它会返回一个字符串

C:\Users

答案 1 :(得分:0)

尝试使用以下内容:

string path = "C:Users\\KHAIRA\\Desktop\\heitech\\Hibah Total v1.2\\Secure\\PDF Folder";

答案 2 :(得分:0)

试试这个

String path=@"C:Users/KHAIRA/Desktop/heitech/Hibah Total v1.2/Secure"

String fullpath=path + "\\\" + "PDF Folder"

Fullpath将包含您想要的路径