Read values from non-root web.config file - asp.net

My site is structured in this way:
Root Directory
Arcade
default.aspx
web.config
default.aspx
web.config
I have a method Method1() which accesses:
System.Configuration.ConfigurationManager.AppSettings["Total_Unique_Plays_Required_For_High_Score_Board"]
This value exists in the root/Arcade/Web.config file but not in the root/web.config file.
When I execute Method1() from a page in the /arcade directory it works fine. However, when I execute this method as a timed event from global.asax it searches for the value in the root web.config file and throws a System.NullException.
Does anyone know how I can specify to search for the value in the root/arcade/web.config file and not the root/web.config file?

You can open the web.config file first.
So calling this will load your child file; notice you give the path to the folder containing the web.config, not the actual config file.
var config = WebConfigurationManager.OpenWebConfiguration("~/Arcade");
You can now get your values, like:
string MyValue = config.AppSettings.Settings["MySetting"].Value;
You can also get the list of app settings by calling:
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;

Related

mkdir() doesn't work in Dynamic Web Project

I'm trying to upload files using a servlet, which creates a new directory and insert the file into that. Now I'm trying to create the directory, but the problem is that the path doesn't match. Can you help me?
String path="imageProdotto"+File.separator+"test";
String finalPath =getServletContext().getRealPath("")+ path;
File uploadDir = new File(finalPath);
if(uploadDir.mkdir())System.out.println("ok");
else System.out.println("no");
So, when Servlet is called, mkdir() return false.

How to find the Config File location via ConfigurationManager?

How to find the Config File location via ConfigurationManager?
I have the ConfigurationManager class in code and I'm debugging it. I'd like to know to which config file it's pointing to (web.config or app.config, etc).
Is there any property or method on the ConfigurationManager that can help with this?
The configuration file itself is represented by Configuration object. To get this object, run this:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Then you can view the file path via config.FilePath.
Update. As pointed out by Schadensbegrenzer for web application you will need another code to load the config file:
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

How to play sound located in different folder in flex?

I can access and play file named "Song.mp3" located in the root directory of my project but when I put it into "assets" directory I got error.
Here is my project directory:
- Test
-- src
--- Homeview.mxml
-- assets
--- Song.mp3
-- Song.mp3
I can access and play Song.mp3 located in the root folder but I get error when I want to play assets/Song.mp3.
This works:
var req:URLRequest = new URLRequest('../Song.mp3');
This does not:
var req:URLRequest = new URLRequest('../assets/Song.mp3');
I get following Runtime error:
Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: app:/assets/Song.mp3
I think that Song.mp3 in the root directory get exported in the final swf file but assets/Song.mp3 does not. If that is the case is there a way to force Flex to export assets/Song.mp3?
You can use file class i.e. File.applicationStorageDirectory.resolvepath("your url")
or File.applicationDirectory.resolvepath("your url").Store it in File type variable;use the file.url or file.nativePath to access the file.
This will be able to retrieve your external file which is there in the applicaton folder.

How to use LoggingSettings to read 'loggingConfiguration' from Web.Config

I have the following code to read the 'loggingConfiguration' from web.config file that is using EntLib 4.0
Configuration entLibConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration
(#"/Web.config");
LoggingSettings loggingSettings =
(LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);
The loggingSettings object is null after the 2nd line executes.
Any suggestions on what is going wrong here?
ExeConfigurationFileMap exc = new ExeConfigurationFileMap();
exc.ExeConfigFilename = #"Web.exe.config";
Configuration _config = ConfigurationManager.OpenMappedExeConfiguration(exc,ConfigurationUserLevel.None);
LoggingSettings log = _config.GetSection("loggingConfiguration") as LoggingSettings;
Try this, it works for me.
The reason why you get null returned for LoggingSettings is that there is no LoggingSettings configured in the web.config that you are opening. This is probably because the path specified is incorrect.
I have a web appliation set up with 2 web.configs: The first in the root and the second in a Config folder.
/Root
web.config
/Root/Config
web.config
The web.config in the Config folder contains the LoggingSettings. The code to read the LoggingSettings from a page that is not located in the Config folder is:
Configuration entLibConfig =
WebConfigurationManager.OpenWebConfiguration(#"~/Config");
LoggingSettings loggingSettings =
(LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);
This should work in the Development Web Server as well as IIS.

How to get the uploaded file path?

I am using input tag type="file" to browse the file in asp.net.
I browsed the file "Linq2sql.zip" from the location "c\Desktop\Karthik\Linq2sql.zip".
i can get the file name and path using
HttpPostedFileBase file;
var filePath = Path.GetFullPath(file.FileName);
But File path is like = C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\DevServer\\10.0\\Linq2sql.zip
i have to get the original file path c\\Desktop\\Karthik\\Linq2sql.zip. How can i get?
You can not get the original path of the file on the client system; that information is not sent by the client.
The reason you get what you do with GetFullPath is because that forces a resolution with the simple file name alone with the current directory of the asp.net process. That info is utterly meaningless - and in fact incorrect - in this case.

Resources