Visual Studio 2013 windows forms application with Resource file using c# - c#

I have created windows forms application in C# VS 2013 with resource file (to maintain configurations). I created a Task in 'windows task scheduler' with the exe of the application (I copied 'exe file and resouce related file' to another envirnonment). when I run task, its running without any issues.
My intention of using resource file is to change configurations at any time without building the application.
But when I change configurations in application, if I update resource file in above mentioned environment, application using old configurations. when I copy new exe file its working with new configurations.
Please let me know is it correct way or guide me how to schedule 'windows forms application' in 'windows task scheduler' with resource file.
Thanks in advance,
Krishna.

You're confusing resx files and config files. You should use the second one. Here is good example.

Outside of ASP.NET, there isn't an easy way to use resource files in this way, resource files (.resx) are XML files which are build into a binary formal .resrouce file and embedded into the application assembly during build time, this is why any changes are ignored.
However you can put settings in your .config file in the appsettings section or build your own configsection, this way when the application runs again it will read the settings with your changes from this file.
You can also have a custom Xml/Json/Ini file and read from it using with your own code, but for simple settings, use appsettings section in the app.config file.
https://msdn.microsoft.com/en-us/library/aa903313(v=vs.71).aspx

Related

Access to an embeded resource file doesn't work on Xamarin but on Windows App

I added an excel File (template.xlsx) to the project resources as an embedded resource. In my code I want to save that file to disk.
On Windows it works well with:
File.WriteAllBytes("test.xlsx", Properties.Resources.template);
But in my Xamarin.Forms project exactly the same line does not work because "Properties" is unknown in the current context.
I searched half a day in the net but all hints I got don't work either or confuse me even more. It seems I am missing an essential piece of base knowledge here. Could somebody tell me if I can easily access an embeded File in Xamarin similar like I do on Windows?
The difference I can see in the two test projects is:
In the project explorer of the windows project I can see Properties->Resources.resx. However, the template file is not located there but in Resources->template.xlsx
In the project explorer of the Xamarin project there is no Resources.resx in the Properties folder.
My file is again located under Resources->template.xlsx
You have to read that file in your code and then use it to write that file to file storage (application sandbox) of the platform. You can save files to that location, although you may not easily be able to access that file outside the scope of the app (say from another application).

Is the arrangement of an application reading App.config specific to Visual Studio IDE and/or compiler?

I heard that for C# programs,
App.config —This file contains configuration settings that the application reads when it starts.
For applications compiled from C programs, they don't always read some files in a specific directory. Whether that happens is up to the applications.
Is the arrangement of an application reading App.config in a particular directory done by the compiler?
Is the arrangement specific to Visual Studio IDE or compiler?
If the application is compiled not within Visual Studio, will the arrangement still hold?
Thanks.
Good question. As I understand it, the reading of the configuration is handled by classes in the System.Configuration namespace, and by convention they search for the .config file in the same directory with the same name as the executable you are running. You can use System.Configuration's ConfigurationManager to open arbitrary .config files, but that's uncommon in my experience.
From the link:
Executable–hosted app.
These apps have two configuration files: a source configuration file, which is modified by the developer during development, and an output file that is distributed with the app.
When you develop in Visual Studio, place the source configuration file for your app in the project directory and set its Copy To Output Directory property to Copy always or Copy if newer. The name of the configuration file is the name of the app with a .config extension. For example, an app called myApp.exe should have a source configuration file called myApp.exe.config.

Add resx to cab file

I have an app on Windows Embedded which uses .resx files to translate the app to different languages.
Also I create an installation .cab file but I can't include the resx file to this cab.
How can I achieve this?
Thanks for any tip
A few things:
You'd not told us how you're trying to add the file. Are you using a custom INF file and just calling CABWIZ or are you using a Visual Studio Installer Project?
What have you done to try to include the file?
Most importantly, a RESX file does not contain the run-time resources and you rarely would deploy it. The RESX resources get compiled into a *.resource.dll assembly, that is typically in a subfolder with a name for the locale (e.g. en-us or fr-ca). You need to deploy those files/folders which is challenging because CABWIZ doesn't allow duplicate file names (and all resources have the same file name, just different folders). That scenario is handled by this SO question.

Using app.config correctly

In several C# projects I have been using an app.config file to pass various settings to my program, settings like connectionstrings, power levels etc.
However sometimes I have come in situations where the settings aren't updated as expected and I have concluded that I am not that well informed with the proper use of app.config file.
Example:
Replacing the .exe file with a new version (where settings are different) to the output directory without changing the exe.config, results in the program seeing the hard-coded settings and not the settings of the existing .exe.config
So my Questions are:
What is the exact role of exe.manifest file
Every time I create a new .exe do I have to paste in the output folder anything else except the .exe file?
Whats the difference in obtaining the setting value by: ConfigurationManager.'settingName'... rather than:
Properties.Settings.Default.'settingName'?
What is the role of app.config build action?
Sorry If I am asking too much in a single Question.
The app.config file is a file that gets a special treatment when running the associated application. If you executable is named MyApp.exe the app.config file should be named MyApp.exe.config. The role of the app.config build task is to copy the file named app.config in your project to MyApp.exe.config in the output directory.
.NET provides ways to read the contents of the file (it is in XML format) and various parts of .NET will look for different sections in this XML to provide configuration.
A widely used section is the settings section you refer to as Properties.Settings.Default. This section plays together with Visual Studio that provides an editor for application settings. The settings are accessed in the code by using a generated class. Adding a setting will add a property to this class and this property is initialized from a value in the app.config file.
In the Visual Studio editor you can set a value for the setting and you can think of this as a default value for the setting. However, if you overwrite the value in the app.config file the later will take precedence. This allows you to modify the app.config file after installation and rerun the application with a modified setting.
You can also access application settings the app.config file using other methods, but in my oppinion using the Visual Studio editor and the code generated class is the best way to do that.
I am not sure I fully understand the problem that you experience. If you update MyApp.exe and leave MyApp.exe.config intact you should not see a change in the settings used by the application (unless of course you have renamed or changed some settings).
The manifest file provides information about side-by-side assemblies and can be used to request elevated privileges among other things. This is not related to the app.config file.
There quite a few resources about that.
See http://msdn.microsoft.com/en-us/library/ms229689%28v=vs.90%29.aspx
and the (better) overview: http://msdn.microsoft.com/en-us/library/k4s6c3a0%28v=vs.110%29.aspx
app.config is a very powerful tool. It addresses many issue like versioning, migration, upgrading etc. but this requires some in-depth reading from the links above.
Maybe one thing you could do, if you want to copy only .exe file every time you build your app, is make a settings.ini or settings.txt file, put your parameters in this file (that are not secret of course) and read all your settings from there when you start your app.
You can put all your connection string logic in your login form if you have one...

Include config file after publish with WiX

Scenario:
I have two applications, a Windows Forms App and a Windows Service App. The two applications work together, use same libralies (dll) and share the same configuration file (this file is not the app.config but a custom file).
Complications:
I have a website (webforms) where the user will enter information about configuring the software, this information will be saved in the database and from this information will be generated the configuration file. The site should generate build the project with the new configuration file and the page responds to the client's request with a link to download the .msi.
Problem:
How to generate an installer from a command line to be called by the web application after generating the configuration file. I researched and found the Windows Installer XML (Wix), but it seems to be necessary to compile the entire project every time someone downloads. It's possible leave the program compiled and only add the configuration file after?
Apretiate any helps
Light (the linker in the WiX Toolset) has a feature called "cab cache" which will re-use the cabinet files which are embedded in the resultant MSI. You would use the arguments -reusecab and -cc to enable this.
You'll still have to re-build the MSI when the user submits your form, but the build will be faster (cabinet generation is usually the longest part of the build process).

Categories

Resources