简体   繁体   中英

How to get file location in C# from folder

In My project ,i have a folder with Details.aspx page. and i have Details.xml file outside of the folder. Now i want to get file location of Details.xml to Details.aspx.cs page. i have tried different ways, but i am not getting file location.

Details.aspx.cs :

 private void GenerateXMLFile()
    {
        try
        {
            DataSet dsJobsDetails = new DataSet();
            dsJobsDetails = GetJobDetails();               
            string fileLoc = Server.MapPath("Details.xml"); 

            if (File.Exists(fileLoc))
            {
                try
                {
                    dsJobsDetails.WriteXml(fileLoc);
                    Response.Redirect("Details.xml");
                }
                catch { }
            }
        }
        catch { }
    }

Please tell me how to get file location. Thank you..

string fileLoc = Server.MapPath("~/Details.xml");

Will give you the full path to the Details.xml file on your server, something like C:\\inetpub\\wwwroot\\application\\Details.xml .

You can return that as URL by redirecting to it for example, using ~/ (the application root, in URL form):

Response.Redirect("~/Details.xml");

This will translate into a redirect to something like http://server/Application/Details.xml .

For example your file structure is like this

/Details.aspx.cs
/MyFolder/Details.xml

Then you can get the file location by this

string fileLoc = Server.MapPath("/MyFolder/Details.xml");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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