accessing App.Config file after deploy in C# - debugging

Im using below code to update app.config's some values( I have config file path in the app.config file).When deploy its getting errors I think its becouse app.config file change in to an exe. how to change my code work as debug time as well as deploy time
var appPath = ConfigurationManager.AppSettings["configPath"].ToString();
string configFile = System.IO.Path.Combine(appPath, "App.config");
var configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
config.AppSettings.Settings["InvoiceInterval"].Value = InvoiceIntervalVal.ToString();

Hi thanks every body for instance reply. I fix my own problem it was just a confusion. I was a java guy and I new to .net in .net App.config file compile and create .config in Debug folder file even though debug it access that .config file in Debug folder . So actually when if you change the value in App.config in programatically it doesn't change the App.config file. it change the .config which is in debug file.its like [project name].vshost.exe.config in debug folder.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["InvoiceInterval"].Value = InvoiceIntervalVal.ToString();
config.AppSettings.Settings["directPaymentInterval"].Value = directPaymentIntervalVal.ToString();
config.AppSettings.Settings["paymentStatusInterval"].Value = paymentStatusIntervalVal.ToString();
config.Save();
ConfigurationManager.RefreshSection("appSettings");
using above code you can able change App.config file's value in debug time also in run time. but those changes not seems in App.config file. but you can see changes in exe file which it is belongs to.In my case it was in src\Vetserve.Credicare\bin\Debug\Vetserve.Credicare.vshost.exe.config

Perhaps try and set your App.config's file 'Copy to Output Directory' property to 'Copy always'.
Reference: AppConfig file not found in bin directory

After compilation App.Config will be available as
YourConsoleApplication.exe.Config inside application bin.
You can do like:
var beforeInvoiceInterval = ConfigurationManager.AppSettings
["InvoiceInterval"].ToString();
ConfigurationManager.AppSettings["InvoiceInterval"] = "Your value";
var afterInvoiceInterval = ConfigurationManager.AppSettings
["InvoiceInterval"].ToString();
afterInvoiceInterval will contain the value you assigned but it'll not
modify YourConsoleApplication.exe.Config.

Related

Use value from application.yml file inside build.gradle

I am wondering if it's possible to have a value inside my application.yml file:
app:
constant:
foo:
fooValue: 'test'
and to get this value and put it into a variable inside my build.gradle file
What I've tried so far:
How to load yml propery to gradle
I tried the solution in the above link, however
def app = new org.yaml.snakeyaml.Yaml().loadAll(new File("src/main/resources/application.yml").newInputStream()).first()
the above in my build.gradle throws an error 'src\main\resources\application.yml (The system cannot find the path specified)'
Does anyone know how I can fix the error above or of a better solution?
In case anyone wants to know, adding $projectDir to the start of the file path fixed the issue.
def app = new org.yaml.snakeyaml.Yaml().loadAll(new File("$projectDir/src/main/resources/application.yml").newInputStream()).first()

Cannot find content file in bin folder

I have a IIS Express project running in debug mode in Visual Studio 2015.
The web application references a Class Library which has this line of code:
var certificate = new X509Certificate2(#"certkey.p12", "notasecret", X509KeyStorageFlags.Exportable);
The certkey.p12 file is in the same folder as the source code for the class library as is marked as "Build Action = Content" and "Copy if newer".
When I build, the file is copied to the web app's bin folder as expected. But when I call the method from the web app, it throws "file not found".
It works fine in Unit Testing where the file is copied to /bin/x64/Debug/
So where is IIS Express looking for the file? I suspect it is some sort of temp folder but why isn't VS copying the file there when I build/debug?
When running under IIS Express, it was looking for is "C:\Programs Files\IIS Express\certkey.p12".
I used a little peeking to find the folder that the file was actually copied to and came up with this:
string binPath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
Uri uri = new Uri(binPath);
string localPath = uri.LocalPath;
string dir = System.IO.Path.GetDirectoryName(localPath);
// now "dir" is the full local path of the bin folder
It is a little slow on first execute (~100ms) but faster on subsequent requests.
If there are any problems when I deploy this to production, I'll update.

Is there a way to define the local profile in the config for Protractor?

I have Jasmine-Reporters set up in my config files and working great for me, locally:
require('C:/Users/**<me>**/AppData/Roaming/npm/node_modules/jasmine-node/node_modules/jasmine-reporters')
var outputPath = "C:/Users/**<me>**/<myPath>/"
jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter(outputPath, true, true));
},
What I'd like to do now is to somehow make the path declarations dynamic so that my tests can be shared and run on someone else's machine. I've tried just replacing my local profile with %USERPROFILE% but that didn't work. Any hints as to how to do this?
I think I have this figured out. What I ended up doing was to create a separate 'prop.js' file that just had this one line in it:
exports.username = "<myProfileName>";
Then, in my conf.js files, I can simply do this:
var properties = require('././prop.js');
var username = properties.username;
require('C:/Users/' + username + '/../jasmine-reporters')
That way, anyone on my team that I want to share these tests with only has to edit the prop.js file once with their own profile name.

Unable to locate file 'C:\BuildAgent\temp\buildTmp\SYSTEM_[AGENT NAME] 2013-02-06 16_25_11\

I have an MSTest build step in my TeamCity build. Some of the tests are looking for a file in a relative path which they cannot find and the tests are throwing the error...
Cannot find path...
'C:\BuildAgent\temp\buildTmp\SYSTEM_[AGENT NAME] 2013-02-06 16_25_11\Documents\json.value.list.txt'
I have a PowerShell script that I want to use to create and copy the file out to the above path. However, I cant seem to figure out the appropriate TeamCity parameters to use to construct the path...
I have...
%system.teamcity.build.tempDir%\%teamcity.agent.name%\documents
However, that gives me...
'C:\BuildAgent\temp\buildTmp[AGENT NAME]\Documents\'
What can parameters or TeamCity variables can I use to construct...
C:\BuildAgent\temp\buildTmp\SYSTEM_[AGENT NAME] 2013-02-06 16_25_11\Documents
Thanks!
If your test is reading the contents of a file, this will ensure that relative paths are resolved correctly so tests can be run on TeamCity as well as locally:
string currentDir = new System.Diagnostics.StackFrame(true).GetFileName();
var workingFile = new FileInfo(currentDir);
string fileContents = File.ReadAllText(workingFile.Directory + relativeFilePath);
relativeFilePath is the variable containing the file. fileContents will contain the contents of this file when run from TeamCity or locally.

Use relative path in Firefox extension

I develop Firefox extension with bundled executable file which should be run on browser startup.
To run process I need get nsIFile or nsILocalFile instance which points to executable file.
I know one solution how to get it using directory service:
var file = Components.classes["#mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
file.append("extensions");
file.append("<extension id>");
file.append("<relative path>");
But this solution has two disadvantages:
It doesn't work in development mode, when instead of installed extension I have only text file with real extension path
I'm not sure that it will work on all Firefox configurations because of hardcoded "extensions" part of the path
So is there any nicer way to run executable file which comes with Firefox extension?
Thanks.
You are making way too many assumptions about the directory structure of the Firefox profile - don't. The Add-on Manager API lets you get the path of a file inside the extension, you should use it:
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("<extension id>", function(addon)
{
var uri = addon.getResourceURI("<relative path>");
var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file;
...
});
A restartless addon's startup function (in the bootstrap.js file) will receive, as its first parameter, the path where the addon is installed. You can then play various tricks to read files inside the .jar file, if any: see https://github.com/protz/GMail-Conversation-View/blob/master/bootstrap.js#L55 as an example.
In a non-restartless case, I must confess I don't have much of an idea :).
I found this thread looking for a way to reference a path to an image hosted in extension's directory from a content script. Here's a solution:
Include your files in web_accessible_resources in the extension's manifest.
"web_accessible_resources": [
"images/*"
]
Absolute paths to these resources contain randomly generated UUID, therefore we're using runtime.getUrl() giving it the path relative to manifest.json. Example:
let myImg = document.createElement('img');
myImg.src = browser.runtime.getURL("images/my-img.png")

Resources