[Help]Trying to make a console with auto-restart to update Configuration.ini

05/31/2013 20:27 stephanyd#1
Hi... i try to build a console that auto-restart and update Configuration.ini for my server but it won't restart itself. Here is the code .... someone can help me plz?

Quote:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Net;

namespace test1
{
class Program
{
static void Main(string[] args)
{
string MyIP = "";
try
{
try
{
IPHostEntry serverHE;
try
{
serverHE = Dns.GetHostEntry("MyHost.dyndns.info");
Console.WriteLine("Trying...");
}
catch (Exception)
{
MessageBox.Show("Host not found");
Environment.Exit(1);
return;
}
IPAddress IP = serverHE.AddressList[0];
MyIP = IP.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
if (File.Exists(System.Windows.Forms.Application.Star tupPath + @"\configuration.ini"))
{
string[] content = new string[1] { MyIP };
File.WriteAllLines(System.Windows.Forms.Applicatio n.StartupPath + @"\configuration.ini", content);
List<string> Lines = new List<string>();

Lines.Add("[Configuration]");
Lines.Add("IP=" + MyIP.ToString());
Lines.Add("GamePort=5816");
Lines.Add("AuthPort=9958");
Lines.Add("ServerName=ServerName");
Lines.Add("");
Lines.Add("[MySql]");
Lines.Add("Host=localhost");
Lines.Add("Username=root");
Lines.Add("Password=password");
Lines.Add("Database=Database");
Lines.Add("");
Lines.Add(";192.168.1.103");
Lines.Add(";70.42.74.212");
Lines.Add(";unknownpassword");

File.WriteAllLines("configuration.ini", Lines.ToArray());
Console.WriteLine("IP Updated Successfully!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Environment.Exit(0);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run();
{
if (DateTime.Now.DayOfWeek == DayOfWeek.Friday && DateTime.Now.Minute == 20 && DateTime.Now.Second == 00)
{
Application.Restart();
Environment.Exit(0);
}


}
}
}
}
05/31/2013 20:47 pro4never#2
Where does it go wrong?

I expect your problem is you're using File.WriteAllLines multiple times. I haven't used it before but I'm pretty sure it's overwriting the content of the file every time you write.

You should likely be adding all your strings via Lines.Add(text) and then once they are all added you can use File.WriteAllLines

That being said you've given us zero information on where the code has an issue. Breakpoint and get back to us.
05/31/2013 21:00 stephanyd#3
Quote:
Originally Posted by pro4never View Post
Where does it go wrong?

I expect your problem is you're using File.WriteAllLines multiple times. I haven't used it before but I'm pretty sure it's overwriting the content of the file every time you write.

You should likely be adding all your strings via Lines.Add(text) and then once they are all added you can use File.WriteAllLines

That being said you've given us zero information on where the code has an issue. Breakpoint and get back to us.
Hi Pro ... all code work fine .... problem is the Application.Restart won't make application restart. anything missing in my code?
05/31/2013 23:08 pro4never#4
Why are you using it at all? From what I can see this program should be executing your code every Friday.

You can do a couple things.


#1: Leave the program running. Use a timer/while loop or some other method to determine how often your save code is being run

#2: Run a batch file that runs a certain app (which closes when it is done every X hours

#3: Look into Scheduling tasks in windows (likely the best solution if you want it to happen say... once a week)