0

From my first project, I check a checkmark, then this line of code runs:

if (checkbox1.Checked == true)
{
  File.WriteAllText("C:\\True.txt", "fshfgyusfusd");
}



Then from my second project, when the form loads, I wanted it to sync the checkmark, so here is my code:

if (File.Exists("C:\\True.txt"))
{
 checkbox1.Checked = true;
}

Is there another easier way to do this, or this is the easier way, I basically want if the first projects checkmark is checked, then the second form loads, and only check the checkmark when the first form checkmark is checked

  • Please provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) so that others can actually see the context of your issue and **help them help you.** – pensum Nov 08 '19 at 02:50
  • There are a lot of ways but what I would do is setup both projects to use a single database (for checking). – Jerdine Sabio Nov 08 '19 at 02:50
  • Does this answer your question? [Object Sharing between Applications?](https://stackoverflow.com/questions/28335037/object-sharing-between-applications) –  Nov 08 '19 at 03:16
  • That's actually the easiest. But also the worst, you should consider WCF or .NET remoting or Pipes. – Javier Silva Ortíz Nov 08 '19 at 03:58

1 Answers1

1

You do have to have some way to communicate.

If your two programs are going to be running at the same time, you can use a socket, or possibly a mutex. If they are not running at the same time, you will have to save stuff to a file, or a database. Possibly your first app could notify a third party -- most likely a webservice -- and the second app could inquire of the webservice whether it happened.

But for asynchronous communication like you've described, files are probably the easiest.

A few suggestions:

  • Your applications should not write to the root of your C: drive. They should probably stick to either your temporary folder, or a folder under your user profile (C:\Users\CNTowerGUN), or a folder dedicated to the program under C:\ProgramData. Each of the apps should check if the folder exists before proceeding.
  • It may be more useful to write the current time than to write a random string. This will assist if you are debugging. Likewise the second app may want to actually read from the file to check its contents.
  • The app doing the writing should be on guard for the possibility that the file cannot be written, due to insufficient permission or the file being in use.
Ross Presser
  • 6,027
  • 1
  • 34
  • 66