Datasources in C# - c#

Long time reader, first time poster.
Background:
We have developed a small application in C# for creating proprietary binary files for use in our embedded system. The application is very simple, just a few textboxes, comboboxes and checkboxes, and a couple of buttons for creating files. It is distributed to our customers as just an .exe file, meaning, no installation is required, and that is how we want it.
Problem:
I have been tasked by our support staff to implement a way to save some settings and load them again. This data should be retained in between runs of the app. The user should be able to save a couple of these under different names.
The question is:
What are my options? So far I have identified 3 possible solutions:
Plain text files: Cumbersome to work with
Settings file: Visual Studio can use a settings file as a datasource, I have tried this approach, but is stuck. Perhaps not for me?
Microsoft SQL Server Compact: I know very little of this. Can we use our deployment method with this solution?

You could use an Xml file to save your settings. Your application could load these settings and act accordingly. If you are .Net 4.0, you could LINQ that will make your job pretty easy. Have a look at this.

If the data to be saved is small and simple, then I would go for physical files; the overhead in setting up the SQL Server Compact for you application is larger than just serialising a class or struct to XML and saving to file.
On the other hand, if the data becomes more complicated, including one-to-many or many-to-many style of relationship, then you would want to move to the database solution.
To keep flexibility ensure you decouple the save/load code so you can change from file to database if the need arises.

"C# Settings" should work for you. Have you tried it right?
Please see the below links. Hope it helps
Visual Studio Settings file - how does it work?
http://msdn.microsoft.com/en-us/library/aa730869%28VS.80%29.aspx
It worked for me.

The easiest way is to use user or application settings
Add an app.config file to your project and edit the settings like this
<setting name="Setting" serializeAs="String">
<value>This is the setting value</value>
</setting>
http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx#settingscs_topic2

In the past, I have created a small "settings" class.
I have then over ridden the ToString method and also created a parse method.
I have stored each setting in the settings file using a global::System.Collections.Specialized.StringCollection type to store a collection of settings. I use the ToString method to store the setting and the Parse method to read it back in.
Lastly, I use an int type to store the currently selected index.

Related

best way to store strings outside of a program?

I'm currently working on a winforms application that gets information from a user via a textbox. Since this information is not likely to change very often, I would like my application to save the input and load it into the appropriate text box the next time it is run.
I feel confident in my ability to set this up on the form side of things, my question has to do with how to store the values outside of the program. My first instinct is to just use a text file, but the overhead needed to handle IO and reading in the values seems a bit much. I also would rather not have the user editing it outside of the textbox in the program. I tried using string resources, but those are read only. Is there a more elegant solution available?
You need at least a mediocre database client whether it be sql server, mdf, MS Access, SharePoint, Oracle etc. Sounds like a vis studio prog. (winforms). They are designed for IO procedures but requires data architecture knowledge or relational dbs. (RMDBS). Try a lightswitch project (if a VS proj.) to get the basics of db and form data relationships.
I know it's not outside Your program, but you could try settings, it's easy, kinda like the resources ownly in the settings you can change it.
To change settings goto:
Properties and then double click Settings, here you van add strings, bools etc. Make sure you set them to user
To access them through code use:
Properties.Settings.Default.
To save the settings use:
Properties.Settings.Default.Save()

Update app.config file after installer upgrade

I'm currently working on a Microsft Word Application-Level Add-in using C#. My application contains an app.config file. In this file I save user-settings (userSettings-Section) and some data defined by a custom ConfigurationSection. The data stored inside of custom ConfigurationSection is also user-specific.
I access the user-settings as follows:
Properties.Settings.Default.MyUserSetting
The custom ConfigurationSection I'm using like:
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal)
.GetSection("MyCustomConfigurationSection")
My questions are:
Do I even use the app.config the "correct" way? (I know that there is probably not only one correct way, but is "my way" one of these?)
Which ConfigurationUserLevel does Propeties.Settings.Default use? Is there a way of setting/changing it?
And Most importantly: Is there a way to automatically keep the user-specific settings during an upgrade of my application, but at the same time add newly created settings?
I deploy my Add-in using an installer built with WiX. At the moment I use CustomActions, which manually insert code for each added/removed/changed setting. But I was wondering whether there is a built-in way of doing this.
I did some researches the whole day now and I think I can answer my questions for myself:
Q: Which ConfigurationUserLevel does Propeties.Settings.Default use? Is there a way of setting/changing it?
A: It seems that Propeties.Settings.Default use ConfigurationUserLevel.PerUserRoamingAndLocal, what sounds logical as they only contain userSettings... I don't know whether it is possible to change it, but for me it is not necessary.
Q: Is there a way to automatically keep the user-specific settings during an upgrade of my application, but at the same time add newly created settings?
A: I found out that there is an Upgrade()-Method in Settings which should take care of copying user settings between an older version of the product and the new one. For more information see http://ngpixel.com/2011/05/05/c-keep-user-settings-between-versions/. I must confess that I haven't had occasion to test it, since I have no permissions to build the installer, but I will rely on it. I will come back to this post after I tested my solution.
Furthermore I am not using a custom ConfigurationSection any more. Instead I derive from System.Configuration.ApplicationSettingsBase, which means that I am able to handle the custom data exactly like the Properties.Settings, including the Upgrade()-Method. See this post for more information about how to use it.
Hope this helps somebody.

Add languages at runtime

I have a multi-language windows application that uses standard .net localization in resx files.
Now I have a request to add a possibility for user to create his own language files that are not originally supported and add them to the application without recompiling it.
What's the best approach to achieve this?
I'm considering moving the languages to database and then crating a second tool that'll add translations to the database, but would rather keep the current approach, if it's possible to add resx files dynamically.
This is exactly why I don’t use .NET localization but wrote my own.
I store the translations in files separate from the EXE, have a Windows-Forms-based GUI for translation that I can embed in any application (and working on a WPF clone of it), and allow users to switch languages without restarting the application.
I don’t get why anyone would want it any different and why Microsoft created such a bad and limited system.
Unfortunately, I cannot publish my system right now, but I’m hoping I’ll be able to soon.
One option you can use is keep the translations in an XML file. This way, the user can just drop his own XML file into the folder where the translation exists.
You could write the translations to .resx file and just add the location of that file to the database and then when you want to translate some label just go to the database to see where that file is and then read from it. I'm not sure though how it would fit with a localisation lib...

Graphical application.config editor?

I am wondering if .net has some sort of inbuilt gui for editing application.config files.
From googling around it seems that they exist but are designed to be run from visual studio.
What I want to do is to allow admins of the software I am writing to be able to customise the application config from within the software at a customers site.
Could someone advise me if this is possible?
At the minute I thinking I will have to wite my own code to parse the config files to generate the Gui.
Create your own form whcih takes application.config values from user and update that in the application.config

Are there any performance issues or caveats with resource (.resx) files? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Resource files seem great for localization of labels and messages, but are they perfect?
For example:
Is there a better solution if there is a huge amount of resources? Like 100,000 strings in a .resx file? (Theoretically, I do not actually have this problem)
Is this a good method for storing the other types of data, such as images, icons, audio files, regular files, etc.?
Is it a best practice to store your .resx files in a stand-alone project for easier updates/compiling?
Are there any other issues that you have run into when using .resx files?
1. Is there a better solution if there is a huge amount of resources? Like 100,000 strings in a .resx file? (Theoretically, I do not actually have this problem)
I've used Alfresco as an alternative content repository on Java projects. RESX files, from a maintaince standpoint (because of encoding issues I guess) can really stink.
2. Is this a good method for storing the other types of data, such as images, icons, audio files, regular files, etc.?
I've seen it work with images...but that's it. (not sure with other media/files)
3. Is it a best practice to store your .resx files in a stand-alone project for easier updates/compiling?
I don't, but you can edit a resx file on a live site and then edit will go through, I believe. Certainly that's the way it works in development (except for the global resx, I think)
4. Are there any other issues that you have run into when using .resx files?
Besides being really annoying to maintain, and the fact that visual studio doesn't provide the neatest tools for working with them...no.
I recently used a .resx file with 5 million strings (normal length, like this sentence), compiled in different DLLS about 1 GB in size. It still works fine in an Azure web project.
The load time is unknown, maybe few seconds or so, since it always can heat up in stages, I never noticed it.
We have been using resource files on a relatively large .NET Windows Forms application (over 500 various forms, approximately 20 resource strings per form) and we've had no performance issues regarding resources from .resx files.
We have used Babylon.NET as a tool for managing translations (has a free version just for translators).
You did not specify if your project will be web or desktop application. One functionality that resource files offers for desktop applications is the ability to also localize control positions and size which IMHO is not possible using other tools (unless you are using something like DevExpress layout control which has automatic sizing).
Never seen any problems with resx resources, they are being cached perfectly. We have used them in WinForms, asp.net mvc, wpf, etc...
One thing you should do is use the Microsoft MAT (Multilingual App Toolkit) extension for Visual Studio.
You can control your translations, export them to send to translators (e.g. not the locked translations), import them again and verify them or comment on it, recycle existing translations (saving you a lot of time!)
and it works with the industry standard xlf format!
If you sign up with the Azure api you can even automatically translate resources (you have a few thousand words free of monthly credit on azure).
See: https://multilingualapptoolkit.uservoice.com/knowledgebase/articles/1167898-microsoft-translator-moves-to-the-azure-portal
you can even see how much work already has been done in a project:
Oh and it comes with a handy editor which your translators can also use!
To get started:
install the MAT Visual Studio extension
Go to your project in Visual Studio
Click Properties --> open AssemblyInfo.cs
Add this attribute: [assembly: System.Resources.NeutralResourcesLanguage("en")]
Select your project in Solution Explorer and to in Visual Studio to [Tools] --> [Multilingual App Toolkit] --> [Enable selection]
This will add a new folder "MultilingualResources" to your project
Right mouse click your project --> [Multilingual App Toolkit] --> [Add translation languages…] --> select the language you want to translate (e.g. Dutch).
In the "MultilingualResources" folder you will see a new file "....nl.xlf", double click it, it will open with the Multilingual Editor. (if not right mouse click and change the default "Open With" to the multilingual editor)
Now you only add strings to your default Resources.resx file (the language should be same as the "NeutralResourcesLanguage" you added in AssemblyInfo.cs.
For the translations you DONT add strings to the ...nl.resx files, you work with the .xlf files, located in the MultiLingualResources folder.
(after you have done lots of translations, a rebuild might be needed so that the translated .xlf files update the translated .resx files)
Where to get it:
feedback: https://multilingualapptoolkit.uservoice.com/
visual studio extension: https://marketplace.visualstudio.com/items?itemName=MultilingualAppToolkit.MultilingualAppToolkit-18308
knowledge base: https://multilingualapptoolkit.uservoice.com/knowledgebase
github of Cameron (Microsoft) who manages this project: https://github.com/TheMATDude
I have had two problems with resource files, both about performance of the translators (people), rather than the speed of string lookup.
The sales staff at the oversee office
that did the translators could not
cope with editing XML or learning any
new tool.
So they just used Excel to edit the translations. Therefore, we might as well have stored the translated strings as a CVS file, so avoiding having to copy the translated strings into the resource files.
A new build needs to be done so as to
see the effect of any translations.
Once again if the translated strings were stored as a CSV file, we could have cached them in the ASP.NET cache. Then any changes to the translations would show up on the next page load.
So we could have used a custom implementation of the resource provider and keep to the standard ASP.NET resource lookup system. Or just ignore the standard resource lookup system if it does not help in your case – it depends on how your pages are written.
You may find at some point that you wish to be able to override strings for a single customer, if so you will need a multi-stage lookup system. Otherwise, you have to merge the customer’s custom strings with the translated string each time you ship a new version of the system.
For point#4.
I have been using .resx files for all strings on our site that must be localized into many languages and haven't had any major issues with them.
The one thing that you need to think about is if you want this text to be searchable. For some of the sites I work on there are some localized resources that need to be searchable so I must keep them in the database. However, when I have the choice I prefer the .resx file for similar reasons mentioned above.
I will simply add that you should look for custom implementations (or do you own) of the resource provider (provider model like the membership provider) to store your resources in a database. That's what we did for our CMS, and it's very useful.
When we first looked for an example back then we found Creating a Data Driven ASP.NET Localization Resource Provider and Editor.
Here is my take on resource files:
I would assume that if there is a LARGE amount of string, that using a database might be the best method to allow for searching and sorting of the data. It would probably not be too difficult to account for multiple languages in a resource table, and the speed should best fast.
I would think that this is a good method for storing static resources, or things that might be changed by a client. As for dynamic resources, it might be better to use a database, either alone, or in conjunction with the file system. I think in the new SQL Server there is a new type that is an optimal hybrid of using a database and the file system.
I read in another question (don't know which) that using resource files in an external project is a good practice, because you wouldn't have to recompile an entire project when resources change. Just recompile the resource project. This would also allow for (fairly) easy edits to be made by clients, where they would only need to "source code" for the resource project, and not your other real code (API code, etc.).
I have not used resource files enough to make any claims about their reliability, extensibility, or any potential issues that you might have when working with them.
I've been using resource files in a .net razor page app after dumping our previous proxy server that used a custom regular expression language to replace strings as they passed through the proxy.
We dumped the proxy method as it was more suited to large strings (paragraphs) and pretty awkward for all the dynamic fragments and stuff we had.
Had no problem at all and it's faster so far than the proxy server. I store all the target pages, comments, names, en and all other available languages in a DB..trivial to add a new column for a new language.
We have about 5k entries in multiple resx files so far
I then use a builder process to create all the resx files and place them in the correct local and global folders any time something is updated.
Dead easy to build a simple interface for translators to search for pages, languages, comments, names etc and update. We choose not to auto rebuild the resx files on a change but you could if you trust your translators ;)
We also allow translators to add new fragments/text to translate but as yet we've not had any bright ideas on how to include them automatically and have to manually substitute the string in the source file and recompile.
For editing resx files I've used zeta,
https://www.zeta-resource-editor.com/index.html
Which can open all your languages in one go and highlights differences in placeholders and also missing translations. You can edit all the languages on one row and save all the files in one go. We don't use it now as everything is in the DB but recommend it.

Categories