C# application/service to auto-generate a html file to display on local browser

cubangt 1 Reputation point
2023-05-10T19:50:24.39+00:00

This is a 2 part question, for concept and to show actual functionality, im looking to create a small, simple windows application that can run without installation on our console pc that is connected to our meeting room TV.

This application needs to be able to read from a text file or ini file for 2 pieces of data. (Date and Name) we work in a small company and not big on enterprise type applications or such, so until we grow as a company, alot of the things are all localized to the users computers.. So here is what im trying to come up with for our main TV when we all walk into the office.

#1 - small c# windows application that can run locally without installation. This application will need to read from a text/ini config file to pull date and name from the list. Keeping in mind that the purpose of this app is merely to show the manager and owner that the concept works. So need to be able to read the file and pull out the date and name of someone based on todays date. IF there is a records that is within say 2 days before or after today, then pull that info and auto-generate a simple /basic html file that a browser can display. basically this is a very simple "birthday" application, we want to have a html template that can be populate with the users birthday and name to show on the tv without having someone to check every month or week.. The purpose of the app is to demo entering a date and name and clicking a button that generates or uses a html template to show how it works.

#2 - convertering/creating the above but as a service to run on the mini pc that is connected to the TV, it will run in the background and once a day check the file for anyone that has a birthday within 2days before or after today. Populate the html file with the persons name and the TV will alreayd be set to display this file all the time, so once it refreshes, it will show the current birthday.

So creating the application or service i know how to do, what i dont know is how to either generate a new html file with this data and maybe a background image so its festive. OR have a html file that is already created with the picture and just have the name and date added to it so that it can show up on the TV.

What im looking for is suggestions or references on where i can look, everything i find just show how to create HTML, which i know how if i manually create it, but i want this app/service to create the file or update the file.

Is this possible? If so what should i consider or look at for assistance.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
8,222 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 4,256 Reputation points Microsoft Vendor
    2023-05-11T08:13:32.95+00:00

    Hi @cubangt , Welcome to Microsoft Q&A.

    What you're thinking of is totally doable. According to our regulations we cannot make a complete project for you as required. But I can give you some advice.

    The first thing you need to determine is what system is the device connected to the display. (windows or Android, etc.)

    Make sure you have a compatible system before you can create related programs.

    Suppose your program is windows, and there are related libraries that can run winform.

    Regarding the self-starting part, you can create a self-starting project to fulfill your needs. Refer to DisableScreensaverForm.cs

    From the perspective of convenience, it is recommended that you put the owner's information into the project in the form of XML.

    person.xml

    <?xml version="1.0" encoding="utf-8"?>
    <people>
      <person>
        <name>John</name>
        <age>25</age>
      </person>
      <person>
        <name>Jane</name>
        <age>30</age>
      </person>
    </people>
    

    When the program starts, read the xml table and convert the information into a table. Automatically obtain the current date and create a corresponding +-2 date. Traverse the date column and take out all matching data.

    demo.xml

    <!DOCTYPE html>
    <html>
    <head>
        <title>Happy Birthday</title>
    </head>
    <body>
        <h1>Happy Birthday!</h1>
    </body>
    </html>
    

    Open the created template html, put the data in, and save as an opened html file.

    Just open the created file with webbroser.

    If you don't need to shut down the machine, you only need to create a timer control, and then run the above program regularly.

    If your machine is connected to the internal LAN, you can also place the read and created files in other accessible locations.

    I wrote an example of winform, which is executed every time it is started.

    The code is as follows:

    public class Person
    {
        public string Name { get; set; }
        public DateTime Date { get; set; }
    
        public bool IsBirthdayToday()
        {
            DateTime today = DateTime.Today;
            DateTime startDate = today.AddDays(-2);
            DateTime endDate = today.AddDays(2);
    
            int startDateMonth = startDate.Month;
            int endDateMonth = endDate.Month;
            int startDateDay = startDate.Day;
            int endDateDay = endDate.Day;
            int DateMonth = Date.Month;
            int DateDay = Date.Day;
    
            if (startDateMonth <= DateMonth && DateMonth <= endDateDay)
            {
                if (startDateDay <= DateDay && DateDay <= endDateDay)
                { return true; }
            }
            return false;
        }
    }
    
    
    private void Form1_Load(object sender, EventArgs e)
    {
        // Load xml
        XDocument doc = XDocument.Load(@"C:\Users\Administrator\Desktop\person.xml");
    
        // read the XML and create the list
        List<Person> people = doc.Root
            .Elements("person")
            .Select(p => new Person
            {
                Name = p.Element("name").Value,
                Date = DateTime.Parse(p.Element("date").Value)
            })
            .ToList();
    
        XDocument xmlDoc = XDocument.Load(@"C:\Users\Administrator\Desktop\demo.xml");
        XElement peopleElement = xmlDoc.Root.Element("body");
        foreach (Person person in people)
        {
            if (person.IsBirthdayToday())
            {
                XElement p = new XElement("person",
                    new XElement("name", person.Name),
                    new XElement("age", person.Date.ToString("M/d")));
                peopleElement.Add(p);
            }
        }
        xmlDoc.Save(@"C:\Users\Administrator\Desktop\output.html");
        webBrowser1.Navigate(@"C:\Users\Administrator\Desktop\output.html");
    }
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments