When I built my .net site, I retrieved files paths using
Directory.GetFiles(#"D:\project\images");
But when I deployed it on internet server, I have to change the link of this folder, can you help me how can I do that?? can I use relative link in Directory.GetFiles() or how??
Put the path in your web.config. You shouldn't be hard coding paths anyway. What if it changes in the future?
In your settings add:
<appSettings>
<add key="myPath" value="D:\project\images"/>
</appSettings>
...and then call it from your application:
var myPath = WebConfigurationManager.AppSettings["myPath"];
If you really want to go nuts, look into web.config transformations so that when you publish, your release configuration will be transformed and applied for you!
in the web.config have something like:
<configuration>
<appSettings>
<add key="ImagesFolder" value="\Images" />
</appSettings>
</configuration>
then in your ASP.NET C# code behind you can use:
var ImagesFolder = ConfigurationManager.AppSettings["ImagesFolder"];
var files = Directory.GetFiles(Server.MapPath(ImagesFolder));
mind that you need to add a reference to the System.Configuration assembly or you won't be able to add the using statement and access the ConfigurationManager.
in this way there are no hard coded values and you can write the value you want for that appsetting by editing the web.config file in the deployed folder on the web server.
The correct way to do this is by using Server.MapPath
Which maps the virtual path in your web app to the physical path in the server.
Related
I always have doubt regarding how a path is formed whenever we run a windows app.
I have set a key like this in my app config
<add key="LogFilePath" value="..\Log\" />
When i run this from my local machine, it provides the path from where the windows app is run.
But when i run the same project from TFS, and when i try to create a file inside the Log folder , instead of the project mapped path it gives an entirely different path.
Can anyone tell me why this happens?
save the relative path in config and where you want to use it do it like this
string fullPath = Path.Combine(Application.StartupPath,configPath);
App Settings are very straight forward.
Add your properties to your App.Config app settings, e.g.
<appSettings>
<add key="LogFilePath" value="C:\Jaspreet_Files\LoadOrgInPortal.txt" />
</appSettings>
and read them, e.g.
var sqlConnectionString = System.Configuration.ConfigurationSettings.AppSettings["LogFilePath"];
I guess the problem is .. in the value:
<add key="LogFilePath" value="..\Log\" />
This seems to be a relative path, try to get full path first before writing and see where it is writing and where it should.
I have an ashx file that serves PDF documents. Our environment is we develop locally, and the web app is moved to different environments: test and then production.
What is the best way to access a path on the server? How can I use Server.MapPath() in an .ashx handler.
You can access the Server through the HttpContext.
public void ProcessRequest(HttpContext context) {
context.Server.MapPath(...);
}
I'd like to add another case to the scenario
Case one:
For example if you know the path to deploy and this isn't under the context of your application:
from VS 2010 you can add a web.config transform depending on your build configuration
to keep it simple you can have a web.debug.config (let's assume development) and web.release.config (production) or you can set up your own build configuration as web.production.config if you want.
you can create an application setting for referencing the full path of the folder and do a transformation depending on which environment you are going to deploy
something like
<appSettings>
<add key="folderPath" value="c:\dev" />
// other keys here
</appSettings>
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="folderPath" value="c:\production" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
Case two:
using the server mapPath as you mentioned
System.Web.HttpContext.Current.Server.MapPath() or context.Server.MapPath()
If the PDF files are stored in a subdirectory of the one where your ashx is stored, you could use ~ as the root of your application:
public void ProcessRequest (HttpContext context) {
// ...
context.Response.WriteFile("~/PDFs/onefile.pdf");
}
if your files are in a physical folder that is not a virtual directory, you could store the path in Web.Config (with a different configuration in dev, test and production)
My XML file resides in the App_Data folder of my asp.net project. I want to set the path of my XML file in the web.config file so that it can be accessed in my class libraries. I want it because when I move my project to our university computer it makes problem. Please anybody help me write the code in web.config file and also C# code through which it can be accessed.
Also I should mention I have googled this topic and also searched on stack overflow but nothing matched my case
add this in your configuration section :
<appSettings>
<add key="xmlPath" value="C:/Users/Jonesy" />
</appSettings>
then in the code :
string path = ConfigurationManager.AppSettings["xmlPath"];
you may need to add a reference to System.Configuration.
I'm creating a web application, which calls a DLL to run unit tests, I also have another DLL(DataAccessLayer) which performs connections and performs queries to SQL which references the main DLL. Both the DLLs use the same config file to read settings.
When running application from VS, the application is working fine. However when the web app is deployed to IIS, it seems the DLLs are unable to read the settings from the config file.
After some research I found that I might have to explicitly define the configuration elements in the web.config file, however I don't know how to implement this. Can someone please point me in the right direction?
I'm actually retrieving the settings using the ConfigurationManager with the following code:-
public string GetValue(string key)
{
var appConfig = ConfigurationManager.OpenExeConfiguration("path to dll");
strKeyValue = appConfig.AppSettings.Settings[key].Value;
return strKeyValue;
}
Thanks.
Use WebConfigurationManager.AppSettings["HelloWorldKey"]; to read AppSettings from the web.config.
Just set all the appSettings values used by the DLL you mention, directly in the web.config PRIOR to deploying the app. You don't need to modify this at run-time (and you shouldn't anyway, since any modification to the web.config will cause the application to restart)
Add the connectionstring or AppSetting or ApplicationSettings used in you app.config into your web.config, I understand this is a manual task but is the only way that the config will read the settings.
Use following code to access connection string
string filePath= WebConfigurationManager.AppSettings["Pathfile"].ToString();
Web config Fie
<configuration>
....
<appSettings>
<add key="Pathfile" value="Path to dll"/>
</appSettings>
....
</configuration>
I have this C# project in Visual Studio (2010), and I'd like to refer to a file in my home directory in the <appSettings> section of the project's App.config file. That is, I use this syntax:
<appSettings>
<add key="Database" value="sqlite:///C:\Users\arvek\test.db3" />
</appSettings>
Is it possible to refer to my home directory (C:\Users\arvek) via a variable instead of hardcoding it directly? F.ex.: value="sqlite:///$HOME\test.db3".
The ConfigurationManager won't automatically expand anything in the app settings, since they are just free-form strings, but you can do so manually. Use the ExpandEnvironmentVariables method of Environment, which will expand variables of the form %VARIABLENAME% according to the current environment. So:
<appSettings>
<add key="Database" value="sqlite:///%APPDATA%\database\test.db3" />
</appSettings>
string path = Environment.ExpandEnvironmentVariables(ConfigurationManager.AppSettings["Database"]);
The root path to your "home" directory is in the %USERPROFILE% variable, though %APPDATA% is the traditional place to put the kind of thing you're talking about. There is also %ALLUSERSPROFILE% for system-wide data (though in Windows 7 that actuallypoints to a special system-wide data folder, not the "Public" profile.)
I find it easier to just get the app's directory with this simple line of code
string appBasePath = AppDomain.CurrentDomain.BaseDirectory;
That's how I always refer to it.