简体   繁体   中英

C# Store data apart from Database

I want to store couple of strings to be used in my application. But I don't want to store it inside my Database or within Config. The reason is that I want if someone copies the application from one system to another the strings should not be copied over. Also, the strings can be manipulated at runtime through application. What is the best way to do this?

A simple way to store user data is a file inside a user home directory. If the data is supposed for internal use inside your program only, one can use AppData\\Local folder (eg "C:\\\\Users\\\\myuser\\\\AppData\\\\Local" :

File.WriteAllText(
    Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
        "MyCompany", "MyProduct", "MyStrings.txt"),
    "Some strings");

The strings here are stored at "C:\\\\Users\\\\myuser\\\\AppData\\\\Local\\\\MyCompany\\\\MyProduct\\\\MyStrings.txt" .

However, these files would not be readily visible by the user. So you should think about cleaning up such files when they are no longer needed.

One way to do that would be by encrypting your string using a key which is local to that machine eg MAC address , while saving it on the machine. Obviously to use it, you will have to read it and then decrypt it using the same key.

This way even if a string is copied over to another system, the new system will not be able to decrypt and use the string , as it does not have the same key as the original machine.

This solution is independent of whether you decide to store the string in bin or app data or elsewhere on the system.

Simple solution, create a file in user document and store the strings there. It is simple and easy to implement but it does not provide any kind of security. If you are going to store sensitive information then any one can read it. Also the user can delete the file which will result in file not found exception.

Second option is to write it on registry. It is neither hard not simple. Registry will solve the security problem with file. User will not accidentally delete the registry.

You can use the registry for things like that.

I have an Registry-Helper-Class to perform actions like this:

public class RegistryUtils
{
    public static string Read(string subKey, string keyName)
    {
        RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(subKey, true);

        if (registryKey == null)
        {
            registryKey = Registry.CurrentUser.CreateSubKey(subKey, true);
        }

        var keyValue = registryKey.GetValue(keyName);
        registryKey.Close();

        return keyValue.ToString();
    }
    public static void Write(string subKey, string keyName, string keyValue)
    {
        RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(subKey, true);

        if (registryKey == null)
        {
            registryKey = Registry.CurrentUser.CreateSubKey(subKey, true);
        }

        registryKey.SetValue(keyName, keyValue);
        registryKey.Close();
    }
}

Then you can use the class for storing things like this:

var storedValue = RegistryUtils.Read("MY_COMPANY/MY_PRODUCT","MY_STORED_VALUE");

RegistryUtils.Write("MY_COMPANY/MY_PRODUCT", "MY_STORED_VALUE", storedValue);

You can use this to store any kind of value, not only string. But this should not be used for sensitive data, because the user can read and write the values of the registry with the registryeditor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM