如何从文件夹中获取C#中的文件位置

时间:2023-02-05 20:45:08

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页面的文件夹。我在文件夹之外有Details.xml文件。现在我想将Details.xml的文件位置提供给Details.aspx.cs页面。我尝试了不同的方法,但我没有得到文件位置。

Details.aspx.cs :

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..

请告诉我如何获取文件位置。谢谢..

2 个解决方案

#1


2  

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.

将为您提供服务器上Details.xml文件的完整路径,例如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):

您可以通过重定向到URL将其作为URL返回,例如,使用〜/(应用程序根目录,以URL形式):

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

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

这将转换为重定向到http://server/Application/Details.xml之类的内容。

#2


2  

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");

#1


2  

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.

将为您提供服务器上Details.xml文件的完整路径,例如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):

您可以通过重定向到URL将其作为URL返回,例如,使用〜/(应用程序根目录,以URL形式):

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

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

这将转换为重定向到http://server/Application/Details.xml之类的内容。

#2


2  

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");