开发者

Modify actual code in program

i have some variables in an app like : public int temp = 10 ;

Is there a way that i can modify my temp variable so that my temp variable will contain on restart of the app the last value that was stored in it ? I would like to do this without a config file.

(i.e. i would like at some point to modify my temp like : temp = x; where x is an integer and after i close my app and launch it again,the temp v开发者_运维百科ariable should contain x and not 10 )


Well it doesn't have to be a "config" file, but it's clearly got to persist the data somewhere. Modifying the executable itself seems a rather drastic approach, to be honest... common storage options include the file system and the registry... what are you trying to do that wouldn't be adequately solved with those more conventional approaches?


You need to persist this value somewhere. Depending on the type of application you are developing (WinForms, ASP.NET, ...) this somewhere might vary. For example if this is a Windows application you could use the Application Settings.


I wouldn't recommend doing this for a production application but if it's just morbid curiosity then I suggest you have a read at this tutorial: Modifying the IL at runtime. It's not a tutorial that I've managed to get all the way through but a while back I was interested in playing with IL/CLR and tinkered with this.

Anything you want to use it for in production should persist in the variety of available storage options you can use: config files, database, file system, registry, etc. Modifying the executable is a big thing and shouldn't be considered lightly.


You can use the Settings object in the application to handy read/store values


It seems there's a security reason for your question. If you don't want to have the data on disk (even encrypted) and the computer would not be restarted, another process can hold the value for you.

There are many options for inter-process communication but you need to pass data encrypted.


Sounds like you're looking for Orthogonal Persistence. There are very few programming languages that support such feature. And I think there is no mainstream languages that support Orthogonal Persistence. Ref: Orthogonal Persistence article.

For C#, you must at least use config files,registry, or database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜