Using Directory.GetFiles on my .net site

When I built my .net site, I got the file paths using Directory.GetFiles (@ "D: \ project \ images");

But when I deployed it to an Internet server, I need to change the link of this folder, can you help me, how can I do this? can i use relative link in Directory.GetFiles () or how?

+1
c #
source share
3 answers

in the web.config file there is something like:

<configuration> <appSettings> <add key="ImagesFolder" value="\Images" /> </appSettings> </configuration> 

then in your ASP.NET C # code you can use:

 var ImagesFolder = ConfigurationManager.AppSettings["ImagesFolder"]; var files = Directory.GetFiles(Server.MapPath(ImagesFolder)); 

remember that you need to add a reference to the System.Configuration assembly or you cannot add a using statement and gain access to the ConfigurationManager .

this way there are no hard-coded values, and you can write down the value you want for this appsetting by editing the web.config file in the expanded folder on the web server.

+1
source share

Put the path in your web.config. In any case, you do not have to be hardwired coding methods. What if this changes in the future?

In your settings add:

 <appSettings> <add key="myPath" value="D:\project\images"/> </appSettings> 

... and then called from the application:

 var myPath = WebConfigurationManager.AppSettings["myPath"]; 

If you really want to go crazy, check out the web.config transforms so that when you publish, your release configuration will be converted and applied for you!

+3
source share

The correct way to do this is with Server.MapPath, which maps the virtual path in your web application to the physical path on the server.

+1
source share

All Articles