VB.Net How to move the app.config file to a custom location - vb.net

I have an application that has a load of values in its app.exe.config file. The application is used by a few users, and the settings would change on a regular basis. so im having to change the config file, and send it out to all users.
I'd love to move the config file to the network somewhere and point the app to this file. ive tried to use;
Imports System.Configuration.ConfigurationManager
OpenExeConfiguration("I:\app config\HelpDeskQuickCallLogger.exe.config")
But i cant get it to read in the values.
Anyone any ideas?

This is how we handle this requirement if a specific configuration file (sSpecificConfigurationFile) is specified:
Dim oConfig As System.Configuration.Configuration
If sSpecificConfigurationFile.EndsWith(".config", StringComparison.InvariantCultureIgnoreCase) Then
Dim oMap As New ExeConfigurationFileMap
oMap.ExeConfigFilename = sSpecificConfigurationFile
oConfig = ConfigurationManager.OpenMappedExeConfiguration(oMap, ConfigurationUserLevel.None)
Else
oConfig = ConfigurationManager.OpenExeConfiguration(sSpecificConfigurationFile)
End If

I am not sure if this is what you are looking for but see if this Code Project Article helps.
Description from above article:
This article demonstrates how to write a custom Settings Provider to
allow you to persist your My.Settings to your own storage system.

Related

C++/CLI StreamWriter cannot specify path

i' m trying to make loginapp in c++/cli with users data log. I would use fstream but it isnt possible in cli. So I use StreamWriter and i cant specify totally normal path C:\log.txt (also tried) C:\\log.txt And when i debug my programme i get error 'access is denied' but also i have this problem when i run it like an admin. However when I just specify it like a log.txt it works and the file is being maked in vs project folder and its not satisfying for me. I paste my code below.
StreamWriter^ log = gcnew StreamWriter("C:\log.txt", true);
log->WriteLine(newLogin);
log->WriteLine(newPassword);
log->Close();
label7->Visible = true;
I also tried a method with # operator but it also doesnt work. Sorry for my english.
C:\ is a path with admin permissions required. Try launching Visual Studio as administrator or change your path to some other which does not need them.
The documentation seems to say that the StreamWriter constructor will only accept a filename, not a full path: https://msdn.microsoft.com/en-us/library/19czdak8.aspx
The functionality that you want is available with another class, namely System.IO.File: https://learn.microsoft.com/en-us/dotnet/api/system.io.file?view=netframework-4.7.1

How to configure cache folder for SourceDiskCache?

I understand from the documentation that the SourceDiskCache folder cannot be configured using the XML configuration file and is only available "through code installation". However I can't figure out how!
I need to configure a custom folder. I have tried a few different things, with different results (both in Application_Start):
This doesn't throw an error, but uses the default folder (/cache)
var sourceDiskCachePlugin = new SourceDiskCachePlugin {VirtualCacheDir = "~/App_Data/cache"};
Config.Current.Plugins.GetOrInstall(sourceDiskCachePlugin);
This (and most other variations I have tried) throws the error "SourceDiskCache settings may not be adjusted after it is started."
new SourceDiskCachePlugin().Install(Config.Current);
Config.Current.Plugins.Get<SourceDiskCachePlugin>().VirtualCacheDir = "~/App_Data/cache";
How can I configure this?
Also, the documentation states that SourceDiskCache is in beta - is this still the case, and will XML configuration ever be available?
This would be the normal way to configure and install it:
var plugin = new SourceDiskCachePlugin()
plugin.VirtualCacheDir = "~/App_Data/cache";
plugin.Install(Config.Current);
If your code is running more than once, use Config.Current.Plugins.GetOrInstall(plugin); It's best if you only install the plugin during Application_Start.
However, approach #1 from your question should work equally well, as long as you've set the right NTFS permissions on App_Data.

Location of app.config file used by referenced library for My.Settings?

If I have a class library with an app.config file (I know it's not ideal, just bear with me for a moment) which has settings values created by using the projects Settings tab and accessed like this:
Public Shared Function GetMySetting(key As String) As String
Dim value As String = My.Settings.Item(key)
If value = String.Empty Then value = "Setting " & key & " not found."
Return value
End Function
I then retrieve the settings from an application like this:
sb.AppendLine("GetMySetting: " & Library.Settings.GetMySetting("SettingC"))
The settings from the app.config file of the library project are definitely not copied into the app.config file of the application but I can still retrieve the My.Settings from the libraries app.config.
So I added a GetConfigFileName function to the library:
Public Shared Function GetConfigFileName() As String
Return AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
End Function
and retrieved it in the application:
sb.AppendLine("Library Config File: " & Library.Settings.GetConfigFileName)
but that returns the application's config file.
How can I determine which .config file the library is reading from when it calls My.Settings...?
Or are they compiled into the DLL?
but that returns the application's config file. How can I determine
which .config file the library is reading from when it calls
My.Settings...?
By default, it will be always the main.exe.config.
Take a look at this for detailed answer
C# DLL config file for
more informations
You can use external config files. This article on MSDN
explains how.
EDIT
You're misunderstanding the situation, reread this line: The settings
from the app.config file of the library project are definitely not
copied into the app.config file of the application but I can sti retrieve the My.Settings from the libraries app.config.
You are correct. Your settings in the dll.config will not be copied automatically in the app.config.
Concerning the value that you are able to get from your dll, I am still looking for more official answer, but the answer seems to be:
the 'old' values (the ones you define at development time) are hard coded. If the
franework isn't able to access or open the config file it will use the defaults
instead. This will always happen if you use settings in a dll.
Take a look at this Settings.Default.<property> always returns default value instead of value in persistant storage (XML file)
Edited after reading comments.
I think I see what your after. The My.Settings will used the compiled values from your class libs settings as default values. I believe these are compiled in. You can reflect the source though to see if it is setting a defaultvalue attribute on them.
You can override these by setting the values in the main exe's application file. If the settings aren't in the exes file, then you will use the default ones you specified in your class libs.
You shouldn't care about the file name. Just assume the settings are either defaulted or were overridden. If you need to know if they were overridden, that's another issue.

Load App.Config at runtime

I am creating a test project using IBM's Rational Functional Tester(RFT) tool(we are using the VB version of it and it only supports till .net 3.5).In this project I am making all database queries using the entity framework.The problem is entity framework retrieves "Metadata", "Connection String" etc from the App.Config file,and RFT won't let me add a App.Config to the project(I guess it is designed that way -- I googled adding an App.Config file to an rft project and came up with nothing), and the Entity Framework requires you to have the app.config file at the entry point.I was building the string to pass to entity framework in code, but my boss really does not like that.
So looking at my options I think any of the below 2 solutions should suffice(or if you have a better solution please advise).
Is their any way to load an App.Config during run-time.
Is their any way to add an App.Config to an RFT project(should
really be my first question).
If you guys could help me with this it will be great.
Thanks in advance.
After a lot of research i found you can load the config file at runtime by using,
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Assembly.GetExecutingAssembly().ManifestModule.Name + ".config");
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var configValue = config.ConnectionStrings.ConnectionStrings["refrenceNameinConfigfile"].ConnectionString;
just make sure your App.config is within the folder where your exe file is running

System.IO.Directory.Getfiles Silverlight 4 is not working

I'm using Silverlight 4 OOB & elevated trust.
I need to get all the filenames on specific directory & populate an object List(Of String)
The compiler throws an error "Method not found" on .Getfiles() method.
Dim files() As String = System.IO.Directory.Getfiles(Path) 'this line is failing..
Help!
The GetFiles is marked as "Security Critical" and therefore cannot be used from your code.
You will want to use the EnumerateFiles method instead. GetFiles is sooo .NET 1.0, EnumerateFiles is much slicker, even in the full framework you'd want avoid this older Array returning API if you can.
As far as I know you cannot directly access the whole hard drive using Silverlight OOB.
Quoting from Silverlight site:
When running in a trusted environment, you can access only files in
user folders, specifically the MyDocuments, MyMusic, MyPictures, and
MyVideos folders. Although this makes sense from a security point of
view, it’s limiting. You want to enable the user to drag their data
from any location. As it stands right now, if you try to drop a file
from a location other than stated above, Silverlight will throw a
security error.
Please refer to this link for details on how to work with the hard drive using Silverlight OOB:
http://www.silverlight.net/learn/overview/out-of-browser-applications/advanced-silverlight-out-of-browser-introduction#Exercise3