Working with Registry in C# 2.0 (Windows Forms)

2020-02-12 18:34发布

I'm a newbie to Windows Forms.

I'm designing an Windows Application in .Net Framework 2.0 in which, I need to Store a UserName and Password somewhere in the System and access them every time I open My Application and Some times I need to change those credentials on User command.

I heard registry is the Best way in doing so. I know Nothing About Using Registry in C# .Net.

So Can you Help me in

How To Get Values which are In Registry, and how to Set a Value to Registry. ??

I'm Using .Net Framework 2.0

7条回答
对你真心纯属浪费
2楼-- · 2020-02-12 18:45

doing it the registry way is the wrong way to go, , i believe do a config file and read this config is much better way to go. but anyway here how you do registry thing

http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-12 18:49

You could try using IsolatedStorage. MSDN Blurb, code sample, and (given you're storing user credentials) an encryption sample. I've linked .net 2.0 samples where possible.

Storing application data in the registry has become unpopular since there are easier alternatives and using the registry can hurt the performance of your users' computers.

查看更多
Viruses.
4楼-- · 2020-02-12 18:51

Here is a good tutorial that will explain read/write to registry.

http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-editing-the-windows-registry

There are few more things you need to know about registry.

  1. Registry consists of five sections of which HKEY_CURRENT_USER stores the settings of currently logged in user. It is recommended that you store the settings under this key. Settings are stored generally in HKEY_CURRENT_USER/Software//

  2. You can store machine wide settings (applicable to all users using the computer) under HKEY_LOCAL_MACHINE key. However, read/write operations to this requires administrative rights. Your application must be running in administrator privilege to write to this key.

  3. Make sure that your registry reading mechanism returns default values if it is not found in the registry as well as write the default values to registry. This way, you will never run out of setting values.

  4. Registry can be edited and can be read. If you are planning to store username/password combination in registry, make sure you encrypt it before you store it. Further, to make sure that it is not used on any other computer also, you should encrypt it with some machine specific information.

Hope this helps you.

查看更多
小情绪 Triste *
5楼-- · 2020-02-12 18:51

I've never used the registry but first thing that comes up on google is this. Seems like fairly easy to understand so have fun with it :)

查看更多
▲ chillily
6楼-- · 2020-02-12 18:53

Check this tutorial with read, write and delete function for registry

Read, write and delete from registry with C#

查看更多
The star\"
7楼-- · 2020-02-12 18:53

You need to ask yourself a couple of questions:

  1. Should only one user be able to use the uid/password?

  2. How safe should the password be stored?

It's quite easy to store information in the registry. http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.aspx

If you just want to store it for one user, store it under HKEY_CURRENT_USER/software/your_company/your_product otherwize store it under HKEY_LOCAL_MACHINE/software/your_company/your_product.

If your password is to be stored safely, there are safer solutions than the registry.

查看更多
登录 后发表回答