简体   繁体   中英

Different behaviour on first launch C# Console app

I've made simple console app to open office documents in free web version of office365. Source: https://github.com/Norrica/OfficeEmulator/blob/master/Program.cs

It just moves file to OneDrive folder and launches default browser with link as an argument. Link contains unique CID:

https://onedrive.live.com/sync?ru=https://d.docs.live.net/{CID}/{fileName}}

Currently, this CID is hard-coded in source, but I want usert to be able to set it once, on the first launch of my app.

TL;DR: How to achieve different behavior for the first launch of a console app?

If it is just for this simple console app, you can add a string field to your class and prompt the user for input after launching the app. This will prompt the user every time he launches the app.

class Program
{
   string _cid;

   static void Main(string[] args)
   {
            ...    
      _cid = Console.ReadLine();
      // you can do some validations for the input here, and ask for another input if it's not valid
            ...
    }
}

If you want to prompt the user only the first time he launches the app, then you will need to store those values in a database, or in memory data store like redis etc. But considering that the app provided is some kind of exercise you can store it in a config for demonstration. Then you can lookup your storage for the user id, and if there is a present CID value for that user id, you'll know not to prompt the user for input again.

Now if you would like to know if it's the User's First time launching the app on that specific machine you will need to store additional information and do additional checks...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM