简体   繁体   中英

How can i hide configuration files for a c# .exe?

I have an app that needs a json file in the same directory, but the file is directly visible and accessible when anyone uses it,and editing it could break my app. How can i hide that file? Im looking for an archive or packaging method thay would still let me execute the exe.

Edit: A single file solution would be ideal, maybe an archive that executes my exe and stores the other file?

You can try hiding the file by using File.SetAttributes :

File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);

If your aplication is very small and doesn't use certain frameworks you can delete the app.config file altogether.

See answer to this question

If you have a few variables in your config, you'll now have to make static class to define them at compile time.

class MySettings{
    public const string Setting1="Value of setting 1";
    public const string Setting2="Value of setting 2";
}

And in your code:

var setting = MySettings.Setting1;//then use your setting in your code

The idea of having a config file is so that you can make changes after the application has been deployed. For complex apps, you may have a different config file for each deployment environment (eg develeopment environment, test environment, live environment, etc)

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