|
There's already a configuration framework in place for the .NET Framework. Read about the IConfigurationSectionHandler interface at http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemConfigurationIConfigurationSectionHandlerClassTopic.asp[^], as well as many configuration section handlers that are already implemented like NameValueSectionHandler[^] and others listed as implementations on the documentation for IConfigurationSectionHandler . Using this framework for configuration files unifies configuration - from runtime settings to application settings - for .NET applications.
To read more about how configuration files work in .NET as well as how to implement your own sections, read Configuring Applications[^] in the .NET Framework.
So, if you wanted a key/value pair configuration section, you could reuse the NameValueSectionHandler like so:
<configuration>
<configSections>
<section name="mysettings"
type="System.Configuration.NameValueSectionHandler, System" />
</configSections>
<mysettings>
<add key="opt1" value="myvalue" />
<add key="opt2" value="somevalue" />
</mysettings>
</configuration> To use this in your code:
using System;
using System.Configuration;
class Test
{
static void Main()
{
NameValueCollection settings = ConfigurationSettings.GetConfig("mysettings");
foreach (DictionaryEntry setting in settings)
Console.WriteLine("{0} = {1}", setting.Key, setting.Value);
}
} It's important that your configuration file match the executable name for your Windows Forms application or Windows Service (i.e., if your .EXE is named myapp.exe, then you're .config file is named myapp.exe.config) and be located in the same directory as your .EXE (this means that libraries - .dlls - use settings from the .exe that loads them), or use Web.config for ASP.NET applications and be located in the virtual host root (/), in the web application's root (/myapp, for example, which inherit settings from the virtual host's root Web.config), or in a sub-directory of your application (also named Web.config, but can only override some sections like <authorization> .
All .config files inherit settings from machine.config that is in the config sub-directory of your Framework installation for whichever version the application has loaded (or is loaded into).
You can find more about this from the link above.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
I recently did this, and you should note that it may not be enough to have type="System.Configuration.NameValueSectionHandler,System" . With multiple versions of .NET installed, I found it to be necessary to expand this to include the strong version information, i.e. "System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" .
my blog
|
|
|
|
|
In some cases, yes it is necessary, but in most cases - including this case (which I've used countless times, as well as defined my own section handlers) - you can specify a partial name depending on the runtime settings on your machine (so long as an assembly is resolvable using a partial name). For example, if your application was loaded into an AppDomain that loaded .NET 1.1, it would use System.dll as you've described above.
If the AppDomain were created under .NET 1.0, the partial name would match System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
In both cases, however, the runtime settings of your machine as well as enforcement of which runtime was loaded (using the <requiredRuntime> and <supportRuntime> runtime config elements) can change this behavior.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Thanks, steve.
Norman Fung
|
|
|
|
|
Thanks. That's very informative and I think it might just do. Our license files look like:
hash=.....
x509path=.....
serial=....
module1=enable&expiry='1/10/2005'&cal=10
module2=disable
...
...
...
Instead, we could:
<configuration>
<configSections>
<section name="securityinfo" type="System.Configuration.NameValueSectionHandler, System" />
<section name="licenseinfo" type="System.Configuration.NameValueSectionHandler, System" />
</configSections>
<licenseinfo>
<add key="encrypedhash" value="........." />
<add key="x509path" value="..." />
</licenseinfo>
<licenseinfo>
<add key="serial" value="..." />
<add key="module1" value="enable&expiry='1/10/2005&cal=10" />
<add key="module2" value="disable" />
<add key="module3" value="disable" />
</licenseinfo>
</configuration>
In application startup sequence:
NameValueCollection securityinfo = ConfigurationSettings.GetConfig("securityinfo");
NameValueCollection licenseinfo = ConfigurationSettings.GetConfig("licenseinfo");
Certificate cert=loadVendorCert(securityinfo);
byte[] encrypedhash=loadLicenseSectionHash(securityinfo);
//decrypt "encryptedhash" with public key from "cert":
byte[] hash=getLicenseSectionHash(cert, encrypedhash);
//Compare hash:
if( !ValidateLicenseSection(hash, licenseinfo)) {
... app config/license section has been tampered with...
} else {
... the rest of the startup sequence ...
}
Any thoughts...?
Norman Fung
|
|
|
|
|
To what end? If you're trying to encrypt communication, simply install a SSL cert on the server with the "Server authentication" OID (from VeriSign[^], Thawte[^], or a handful of others) and encrypt HTTP over SSL (HTTPS). Done. ASP.NET honors this because the SSL handshaking happens before the HTTP protocol.
If you need a way to encrypt data, consider using what ASP.NET already exposes: read about the <machineKey> element, which is auto-generated by default in your machine.config file, and can be explicitly set in your machine.config file, your site Web.config (the one in the root, or /), and your application Web.config file.
For a simle way to generate validation (public) and decryption (private) keys, read How to create keys by using Visual C# .NET for use in Forms authentication[^].
Now, if neither of these scenarios work, then I would recommend you do what you're doing with a slight modification: group your sections together under a section group like so:
<configuration>
<configSections>
<sectionGroup name="mycompany">
<section name="securityInfo" type="System.Configuration.NameValueSectionHandler, System" />
<section name="licenseInfo" type="System.Configuration.NameValueSectionHandler, System" />
</sectionGroup>
</configSections>
<!-- other stuff -->
<mycompany>
<securityInfo>...</securityInfo>
<licenseInfo>...</licenseInfo>
</mycompany>
</configuration> Make sure to change ConfigurationSettings.GetConfig("securityInfo") to ConfigurationSettings.GetConfig("mycompany/securityInfo") , for example.
This helps group related sections together so as not to be confused with others, or just to form a better hierarchy of settings. For example, the <system.web> element is another section group with child elements handled by lots of different section handlers.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
No, not SSL. The intended application is to generate application config files with licensing options and sign them. First you hash the licenseinfo section, then you use your private key to encrypt the hash. The encrypted hash would then become another key/value pair in securityinfo section of the generated application config file to be installed on client's machine.
Clients are given:
1. The application, the dlls and stuff
2. Generated application config file
3. Vendor certificate
On client side, application uses public key (from vendor certificate) to decrypt the hash - which is used to compare with hash computed on licenseinfo section to see if the settings has been tampered with... just dont fall asleep on me yet
Norman Fung
|
|
|
|
|
With specifics, I only offered suggestions for different scenarios.
If all you're looking to do is sign the .config file - an XML file - you might want ot take a look at my article, Using XML Digital Signatures for Application Licensing[^], which discusses the industry-support WS-Signature specification, support for which is in the .NET base class libraries (BCL).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Thanks Heath, I just got a printed copy of your article so I can go over it the old fashion way when I take subway tonight.
Norman Fung
|
|
|
|
|
Hello,
I am having a little trouble with using the windows forms datagrid control. In my program, I need to give the user the ability to select and unselect multiple rows in the datagrid and then perform some action on those selected rows.
Initially, I was hoping to use checkboxes. I made a bool column in my database and added a checkbox column in my datagrid. This appeared to work okay. When I wanted to determine the selected rows, I just iterated through my data table and looked at the value of the column that was bound to the checkbox column. However, I soon found a problem with this approach. I have a "Select" menu, with different options that select thousands of rows in the datagrid (such as select all, select none, etc.) In order to do this, I needed to change the value of many checkboxes at once, and this is very very slow because I am having to edit each row in the corresponding datatable.
I've been trying to figure out other ways to get the same functionality, and I thought that maybe I could just use the built-in select in the datagrid (highlighting the entire row). I was able to select large numbers of rows much more quickly this way because I wasn't modifying any data in the datatable. However, now I have another problem. I don't know how to relate a row in the datagrid to the correct row in the datatable. I've found some code examples such as this:
BindingManagerBase bm = dataGrid1.BindingContext[dataGrid1.DataSource,dataGrid1.DataMember];
DataRow dr = ((DataRowView)bm.Current).Row;
But, this only works for the current row. What if I have a list of the row indexes in the datagrid, and I want to iterate through that list and perform actions on the correct row in the datatable?
Also, I really prefer the checkboxes over the highlighted rows. Is there a way that I can implement checkboxes without having to make a column in my datatable?
Any ideas would be great!
Thanks,
Blake
|
|
|
|
|
as i do,
dataGrid1.DataSource = DataView1
DataView1.Table = "//correct_datatable"
you can get corresponding row via DataView1
Ex:(to get i-th DataRow )
DataView1.Item[i].row
|
|
|
|
|
Hello there.
I'm not sure if this forum is the correct place to post my quest. But I was searching for so long to get any information about a way to show Crystal's database progress from within an application. Business Objects (distributor) says there is no way. Actually the technical support of Business Objects is not worth mentioning, is it?
If there is defacto no way, I wonder why Crystal Reports is such a successful tool. There must be threads or processes on the system that are running invisibly.
So perhaps there's no way to picture the progress at all is there a possibility to catch the end of the crystalreportviewers work? If that I could show a waitcursor at least.
My report app is build in c# (on a windows system of course) and I have installed Crystal Reports 9 Developer Edition.
Hopefully
T. Labenche
|
|
|
|
|
Being a little impatient, aren't you? Posting your question once an hour will not get it answered any faster.
Anyway, CR doesn't expose any kind of event that you can use to monitor its database progress. Since CR itself doesn't know how many records are going to be returned beforehand, there is no way to tell when the progress will end.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
That is what I feared for. Anyway, thanks for your reply.
Another thing: You are not that kind of person who reprimand without reflections, are you? Perhaps a crash of the IE was the reason why I have (unwittingly) posted the message twice. Sometimes I might be impatient, but I am by no means impatient when I'm expecting unkind messages!!!
Even though
Best regards
T. Labenche
|
|
|
|
|
I want to do a simple search function in a richtextbox. I have loaded a file and by using the Find method it's possible to find a text string, highlight the string and extract the line number, but how can I programatically scroll down to the line?
private int findText(string searchText, int searchStart, int searchEnd)
{
int indexToText = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase);
richTextBox1.SelectionColor = Color.Blue;
int lineNo= richTextBox1.GetLineFromCharIndex(indexToText);
return indexToText;
}
|
|
|
|
|
Why not just use the ScrollToCaret() method of RichTextBox ?
Regards,
mav
|
|
|
|
|
Embarrisingly enough I tried that first but discrded as not working. Just to find out that the control needed focus before it worked...
So it was just to add
richTextBox1.Focus();
richTextBox1.ScrollToCaret();
|
|
|
|
|
Hello there.
I'm not sure if this forum is the correct place to post my quest. But I was searching for so long to get any information about a way to show Crystal's database progress from within an application. Business Objects (distributor) says there is no way. Actually the technical support of Business Objects is not worth mentioning, is it?
If there is defacto no way, I wonder why Crystal Reports is such a successful tool. It is hardly reasonable to customers I think. There must be threads or processes on the system that are running invisibly.
So perhaps there's no way to picture the progress at all is there a possibility to catch the end of the crystalreportviewers work? If that I could show a waitcursor at least.
My report app is build in c# (on a windows system of course), VS 2003 and I have installed Crystal Reports 9 Developer Edition.
I'm looking forward to any information.
Hopefully
T. Labenche
|
|
|
|
|
In VC++ when I used to write a Windows Service, I used to parse the command line arguments. If it is '-install' I used to call functions OpenSCManager() and CreateService() to register the service OR DeleteService() to remove it from the SC manager. (winsvc.h )
I am new to VC#... Please tell me the equivalent functions/code piece to do the above task. I am keen on doing this programmatically (if possible, off course) rather than using the installutil and stuff…
|
|
|
|
|
use ServiceInstaller Class
Look into MSDN for help
Sanjay Sansanwal
www.sansanwal.com
|
|
|
|
|
I am trying to use Excel.WorksheetFunction.VLookup but cant use it. Can someone give me an example of how i should use it. I dont seem to be able to get up any of the WorksheetFunction members when using VS which leads me to think that i need to initialise something.
|
|
|
|
|
hi,
Check this article.
http://www.codeproject.com/csharp/csharp_excel.asp
http://www.codeproject.com/aspnet/getsheetnames.asp
**************************
S r e e j i t h N a i r
**************************
|
|
|
|
|
Thanks for your input but they dont use any WorksheetFunctions so i still need help
|
|
|
|
|
Excel.WorksheetFunction wsf = ThisApplication.WorksheetFunction;
Thought i had found all my answers but my app doesnt have ThisApplication defined as i am not using the VS MS office templates. So what do i need to replace ThisApplication with to get this working?
|
|
|
|
|
Hi,
I hope I'm understanding the problem correctly. You can get an reference to the app through:
Excel.Application app = new Excel.ApplicationClass();
Then there are two ways I can think of to evaluate functions:
object result = app.Evaluate("=LEN(\"ABC\")");<br />
double lnVal = app.WorksheetFunction.Ln(System.Math.E);
and don't forget to quit Excel:
app.Quit();
Hope this helps
|
|
|
|
|