Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 31-Mar-11 22:37pm
Edited 22-Nov-11 5:51am
v2
Comments
Albin Abel at 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
mjbohn at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  
Comments
Olivier Levrey at 1-Apr-11 5:27am
   
Good answer. Have a 5.
d@nish at 1-Apr-11 10:28am
   
Thanks :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
v2
Comments
johannesnestler at 22-Nov-11 13:26pm
   
As allways - you suggest a "perfect" solution, but maybe beyond the scope of OP -(kinda new) :-)
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I guess this can help answer your question.
  Permalink  
v2

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

  Print Answers RSS
0 Abhinav S 155
1 Mika Wendelius 151
2 Sergey Alexandrovich Kryukov 135
3 TheRealSteveJudge 114
4 Peter Leow 110
0 Sergey Alexandrovich Kryukov 8,613
1 OriginalGriff 6,536
2 Peter Leow 3,727
3 Zoltán Zörgő 3,456
4 Richard MacCutchan 2,442


Advertise | Privacy | Mobile
Web01 | 2.8.150123.1 | Last Updated 22 Nov 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100