1

I am making a simple program that:

  1. Automatically Runs on Startup (Invisibly)
  2. Finishes task within 5-10 seconds (Invisibly)
  3. Exits

I am a beginner and have one challenge here. There are two programs. One which user can open to change settings (Windows Forms) and another that runs on startup, finishes task & exit. How can I make two programs in a single project?

Also, where to save configuration so that both the programs can read/write it?

Thanks for help in advance.

Update: The program is basically to clean the desktop on startup. Please check attached design of my software as well for better idea.

Software Interface

Kunal Khanna
  • 121
  • 2
  • 10

1 Answers1

2

There are two answers:

  1. You can make a program that reads the arguments in the Main method to decide in which mode it should run. If run without arguments (such as when double clicked), it presents the user interface, and if run with some specific argument, like say /run, it does not present the user interface and instead performs the task you want. You don't specify how you get the task to happen at startup, but it will have to run this program with that argument (/run).

  2. The easier way to do this is to just make a new project for the program performing the task - that also lets you start and debug the task directly in Visual Studio.

If you're making a conventional Windows application, you're free to save the settings wherever you'd like. Saving them under a subfolder of the AppData folder in the user's home folder/"profile" is the recommended way. You can use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) to get the path to the AppData folder and then make your own folder and files within it.

You can also use the registry.

Jesper
  • 7,477
  • 4
  • 40
  • 57
  • Thanks for your response. I completely understood First answer. Can you please guide me how to call program with arguments? It will be called using a file placed in startup folder. – Kunal Khanna Feb 10 '17 at 11:00
  • If you're using the Startup folder, you can [make a shortcut](http://stackoverflow.com/questions/4897655/create-shortcut-on-desktop-c-sharp) to the program - a shortcut can send arguments by providing them after the path to the program in the Target field (`TargetPath` in the question linked). – Jesper Feb 10 '17 at 11:18
  • 1
    I tested passing arguments in Location Field of the shortcut I placed in startup folder. It worked well. Thanks a lot for your help. – Kunal Khanna Feb 10 '17 at 11:19
  • You're welcome. If you want the task to run on a schedule instead of only when the user logs in (which could be months until the next time), [you can create a Scheduled Task instead](http://stackoverflow.com/questions/7394806/creating-scheduled-tasks). – Jesper Feb 10 '17 at 11:21
  • That's a great idea. I can program to clean desktop when the computer is idle for sometime. – Kunal Khanna Feb 10 '17 at 11:25
  • Is there any way to save configuration somewhere just by using exe file picked up from bin? (Just in case I don't want users to install software). – Kunal Khanna Feb 10 '17 at 11:27
  • Reading or changing configuration does not require the user to install a program. If you download an .exe file and run it, and the program asks to read/save configuration in a particular place or in the registry, this will happen. Installation does not change anything about this - for the purposes of this discussion, it just puts the program inside a folder where the user can find it and lets the user uninstall it easily. – Jesper Feb 10 '17 at 11:30