-1

I'm making my own type of Trial mode and I was wondering if there is a way to check if the time information from the text file is the same as the Date.Today?

private void Trial()
{
    DateTime saveNow = DateTime.Today;
    DateTime expiryDate = DateTime.Today.AddDays(30);
    string fileName = System.IO.Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "trailexample.txt");

    if (!File.Exists(fileName))
    {
        // Write the string to a file.
        System.IO.StreamWriter file = new System.IO.StreamWriter(fileName);
        file.WriteLine(saveNow);
        file.WriteLine(expireDate);
        file.Close();
    }
    else if (File.Exists(fileName))
    {
        var lastLine = File.ReadLines(fileName).Last();

        If (saveNow == LastLine) // This doesn't work of course but kinda the Idea. This is what I need help with.
        {
            messagebox.Show("Your trial is over");
        }
    }
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
Sneakybastardd
  • 288
  • 1
  • 5
  • 17
  • I don't see a question here. Are you trying to use this site as a blog? If you're just trying to share information, you need to do it by posting a question first, and then post your solution as an answer to that question. This site is a "question and answer" site, and there can't be an answer without a question first. The [faq] has more information about posting answers to your own question. If instead you're looking for help, you need to explain what the problem is you're having, and ask a specific question about what it is you want us to help you solve. – Ken White May 10 '13 at 02:01
  • Yes you're right. Any moderator can feel free to delete it. I asked a question first and came to an solution myself and thought that I could share it with them like this. – Sneakybastardd May 10 '13 at 02:03
  • 2
    As I said, the [faq] explains how to post answers to your own question. This isn't it. :-) Please roll back your edit and provide the answer properly as the guidelines say you should. You can even accept your own answer as the correct one (as the faq explains). Thanks. – Ken White May 10 '13 at 02:05

3 Answers3

4

Why not something like this? You don't need a text file that someone can modify. Just have your program create a hidden file somewhere. It doesn't even have to have anything in it. You are just checking the creation date on your hidden file.

           else if (File.Exists(fileName));
              {
            DateTime creationdate  = File.GetCreationTime(file);
            DateComparisonResult comparedate = creationdate.CompareTo(saveNow);

            If ((int) comparedate < 0)
              {  
                messagebox.Show("Your trial is over");
                }
       }
David Green
  • 1,210
  • 22
  • 38
  • 1
    That's cleaver. Never thought of that. However, you have to make it compare if 30 days has passed. Your code only compares with todays date. – Sneakybastardd May 10 '13 at 01:57
  • true. check out this posting. add 30 days to the file creation date and check that against today's date: http://stackoverflow.com/questions/1665941/c-sharp-30-days-from-todays-date-for-the-c-sharp-datetime-gurus – David Green May 10 '13 at 02:23
0

I think you're looking for DateTime.Parse() and DateTime.Now

var lastLine = File.ReadLines(fileName).Last();
DateTime time = DateTime.Parse(lastLine);
if(time.Date < DateTime.Now.Date) {
     MessageBox.Show("Trial over");
}
Steven V
  • 16,357
  • 3
  • 63
  • 76
-1

Okay I have found the solution and it works great. Here is if anyone else need to make their own trial app without using registry information or such:

        private void Trial()
        {
            DateTime saveNow = DateTime.Today;
            DateTime expiryDate = DateTime.Today.AddDays(30);
            string fileName = System.IO.Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"623231489374524.txt");
            var lastLine = File.ReadLines(fileName).Last();
            string Enddate = lastLine;
            DateTime TrialOver = Convert.ToDateTime(Enddate);  
            if (!File.Exists(fileName))
            {
                // Write the string to a file.
                System.IO.StreamWriter file = new System.IO.StreamWriter(fileName);
                file.WriteLine(saveNow);
                file.WriteLine(expiryDate);

                file.Close();
            }
            else if (File.Exists(fileName))
            {
                if (saveNow == TrialOver)
{
    MessageBox.Show("Trial mode is over.");
}
            }
        }
Sneakybastardd
  • 288
  • 1
  • 5
  • 17