Update app.config or web.config via code

Asked

Viewed 204 times

2

I am developing a robot that reads all lines of various files contained in a folder. But I need to know which was the last file and line read so I can resume when I need to.

I thought I’d put it in the database, but assuming I have 10 million+ lines, it wouldn’t be cool to keep updating all the time. Then I got the idea to insert it in my configuration file.

<appSettings>
    <add key="LastFileReaded" value="xpto1.sql"/>
    <add key="LastLineIndex" value="29990"/>
</appSettings>

But taking into account this approach would need to update these values, the question is:

Looking at it semantically, is it incorrect to use this approach? And how would I update the value of that key directly on the web.config or app.config?

  • Asked 50sec ago, the person neither reads or criticizes and already negative rsrs

  • I don’t quite understand, you’ll get "+10 million lines" and prefer to save in a configuration file that in a database, which already has its own structure to manage millions of reads/writes?

  • Ricardo I have several SQL files and I will receive all the lines(INSERT) the idea would go through and go running to the end so I need to store... You recommend me manage via database?

  • humm understood... in your SQL files comes the command ready (insert...) or the data comes and you have to assemble the commands to execute?

  • It makes more sense to persist this information in the database... Create a table with this information and keep updating as the execution. Then consult her when executing a new routine

  • @Ricardopunctual already comes the ready commands, but I can’t run SQL directly because they are GIANT files

  • got it, just one more question: which database are you using? maybe a Bulk Insert work better, faster and performative

  • Mysql, I just need to store an index and a filename...

  • published a reply with a suggestion, use the configuration file, but an alterative file

Show 4 more comments

3 answers

0

You can always create a Settings, where you can then update the values simply:

using MyApp.Properties;

// por propriedade
Settings.Default.var1 = "teste";
Settings.Default.var2 = 10;
Settings.Default.var3 = true;
Settings.Default.Save();

// por chave
Settings.Default["var1"] = "teste";
Settings.Default["var2"] = 10;
Settings.Default["var3"] = true;
Settings.Default.Save();

If it’s for simple things, basically storing information in "primitive types" (string, int, bool, etc.), it can be used at will, but if it is something more complex, an object of a non-reprimitive type, it is recommended something more.

  • It is very primitive yes, it is a robot that I will use once in a lifetime... in case I close the software, restart the PC the Settings is still stored?

  • Yes, go on, I think it stays on %userprofile%\appdata\local with the name of the software or company.

  • Good I’ll test...

0

Looking semantically, it is incorrect to use this approach?

Yes and very wrong, mainly the amount that Voce wants to store the file only supports up to 250kb(KLM\SOFTWARE\Microsoft\InetStp\Configuration\MaxWebConfigFileSizeInKB) and has a character print (I don’t remember for sure but something around 30,000).

as @Ricardo Pontual suggested the best solution 'and use database.

out that every time you write on the web.config you reset the application with the new setting.

  • Hudson the idea is not to save all files and lines the idea is to save only the number of the last line inserted and last file in which this line was

  • if you will have "I will have +10 million lines," in db you will have in config also not? pq if config does with 2 lines db also does.

  • @Leonardobonetti I just put another good reason not to use the web.config for this, because you will reset the application and each edition made.

  • Perfect Hudson, thank you !

0

You can create a specific configuration file for this purpose, without writing to Web.config or App.config.

Suppose you create the "import.config" file with this structure:

<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
  <appSettings>  
    <add key="LastFileReaded" value="xpto1.sql"/>
    <add key="LastLineIndex" value="29990"/>
  </appSettings>  
</configuration>  

To read this file, use the class ExeConfigurationFileMap namespace System.Configuration:

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "importacao.config";


Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
    configFileMap, ConfigurationUserLevel.None);

var lastFileReaded = config.AppSettings.Settings["LastFileReaded"].Value;

To change, simply set a value in the key and save using the method Save:

config.Save(ConfigurationSaveMode.Modified); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.