0

I have a question about data handling in C#.

Here is an example to make things clear: I (being the client) need to write filenames into a program so that I can use those filenames later on. However, I need to (on my machine) be able to enter those filenames into a console (that I build from C#) and then those names to be handled internally (so no text documents storing the entered data) so that I can send the same copy of the app to other machines and it still have the names remembered.

Any way that I can accomplish this in C#..?

Any help is much appreciated. Thanks in advance!

  • It is not clear at all what you are asking or what you are expecting as an answer. For any specific problem you are having please include a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Please also read [How do I ask a Good Question](http://stackoverflow.com/help/how-to-ask). Make sure that your questions are specific and not overly broad. – Igor Jul 26 '16 at 19:40
  • What do you mean by "store internally"? Do you mean to save the files to disk or to save them inside your executable? Saving to an executable is not possible nor would it be recommended. – Cameron Tinker Jul 26 '16 at 19:40
  • Don't save them to the executable. It is fishy at best and dangerous at worst. Synchronise the values using some online resource (database or HTTP) or save them in a Settings file that you share with the application. – CompuChip Jul 26 '16 at 19:42
  • Why can't you store the file names in a txt file? – Sam I am says Reinstate Monica Jul 26 '16 at 19:45
  • it's a joke that this topic is closed. – T.Todua Jan 03 '17 at 08:46

2 Answers2

3

No, you cannot modify an executable while it is running; it's a compiled assembly from your code, and saving values to a running executable makes no sense. What you're asking it to do is not possible.

You can store your information in a binary, encrypted file at the very least if you're concerned with security or you can use a database. There are several ways to store locally (i.e. not require a DBMS).

Here's a link to a useful SO post: What is a good choice of database for a small .NET application?

Community
  • 1
  • 1
rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • Well, he actually can store data in his .exe, for example, by creating a "stub" process that will be launched on main program exit and then write binary data to it. (I know this is a dirty miserable hack, but I had to do it myself) =( – Sergey.quixoticaxis.Ivanov Jul 26 '16 at 19:46
0

If I understand your goal, you can use Settings to store data that will persist. Access the project properties and choose Settings. Click to create new settings file. You can add settings right there or you can do so programmatically:

static void Main(string[] args)
{
    //Assuming you added a string setting named "FooSetting" you can set it like this:
    Properties.Settings.Default.FooSetting = "Some Value...";
    Properties.Settings.Default.Save(); //you must call save or it won't persist

    //Read a value like this:
    Console.WriteLine(Properties.Settings.Default.FooSetting);

    Console.ReadKey();
}
Crowcoder
  • 11,250
  • 3
  • 36
  • 45