Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Good day guys…

I want to ask for your help and support on how to create a property file in c#? I've done this in java but I'm not sure how to do this in C# or if there are any ways to do this? I'm kinda new so I don't really know how to start ^_^ thanks for the reply in advance…
Posted
Updated 22-Nov-11 4:51am
v2
Comments
Albin Abel 1-Apr-11 3:40am    
That is something call settings file in c#. I am not sure it is different in java like the UI controls property to a file? Let us wait for a better answer
Michel [mjbohn] 1-Apr-11 5:02am    
If you'd like to store properties in way that users can't modify them, you can use BinaryFormatter to serialize data to file. If this is not the point, app.config is the way to do it, as suggested by d@nish in solution 2.

I think property files store configuration data for the application in Java. We have config files in .net for that purpose. You can use app.config/web.config (based on the application type) to the store the configurable values either as Settings or using a custom configuration section.

If the data you need to store in the config is too large, you can break them down in to XML files and the config file can have the path to those XML files.

Settings[^] in .Net.

Custom configuration section[^] in .Net.
 
Share this answer
 
Comments
Olivier Levrey 1-Apr-11 5:27am    
Good answer. Have a 5.
dan!sh 1-Apr-11 10:28am    
Thanks :)
I think the best way of doing something (actually the closest to what you want as I understand it) is using Data Contract.

You need to create a data type or a set of pure data types. In instances, the will eventually form some data graph of any level of complexity. The question is, how to store it and restore back from file/stream to get exactly same thing?

With Data Contract, in contrast to traditional serialization, you do this absolutely non-intrusively. You only apply to the attribute [DataContract] to each type and [DataMember] to each member you want to persist. You don't even have to persist a tree structure, it could be more general graph of data. Don't forget you unique name space as your company URL to the [DataContract] — it will make your created-on-the-fly data standard world-unique!

The members you apply [DataMember] attribute to don't have to be public or internal, they cannot be even private. This is the whole point: you're responsible only on what's marked as a part of Data Contract. If you change something which is not part of the contract it will not harm your backward compatibility. You can also migrate the Contract incrementally (add members, don delete the legacy ones, better call them obsolete but keep) — in this case you backward compatibility is still preserved.

So, this is acomprehensive tool pretty much for everything. Use System.Runtime.Serialization.DataContractSerializer, System.Runtime.Serialization.DataContractAttribute, System.Runtime.Serialization.DataMemberAttribute. See:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^],http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx[^].

Read this: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

See also past discussion here: How can I utilize XML File streamwriter and reader in my form application?[^].

—SA
 
Share this answer
 
v2
Comments
johannesnestler 22-Nov-11 13:26pm    
As allways - you suggest a "perfect" solution, but maybe beyond the scope of OP -(kinda new) :-)
Sergey Alexandrovich Kryukov 22-Nov-11 13:48pm    
Thank you,

Unfortunately, if you think this is out of scope of OP, what's in scope, then? This is actually the easiest way of storing and loading properties.
--SA
Sergey Alexandrovich Kryukov 21-Apr-16 11:24am    
Maybe... What else then?
—SA
I guess this can help answer your question.
 
Share this answer
 
v2
Comments
Wild-Programmer 21-Apr-16 15:31pm    
"This" is a link. Click it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900