private void Form1_Load(object sender, EventArgs e) { appFilesPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\FileSync"; iniFilePath = appFilesPath + @"\config.ini"; String errMsg = ""; if (!Directory.Exists(appFilesPath)) { try { Directory.CreateDirectory(appFilesPath); } catch (Exception ex) { MessageBox.Show("Could not create the ini file directory '" + appFilesPath + "'. The error returned is: " + ex.Message, "Problem creating the config file folder!", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } } cbInterval.SelectedIndex = 0; SystrayIcon.Visible = true; this.Visible = false; localServer = new FileTransfer.Server(ServerCallback); if (localServer.GetLocalIpaddresses(ref localIPAddresses, ref errMsg)) { foreach (IPAddress address in localIPAddresses) { cbLocalIpAddresses.Items.Add(address.ToString()); } } if (File.Exists(iniFilePath)) { InIFileControler ini = new InIFileControler(iniFilePath); String tmp = ""; ini.GetValue("ID", ref tmp); if (!tmp.Equals("")) { tbUniqueId.Text = tmp; } tmp = ""; ini.GetValue("SourceFolder", ref tmp); tbSourcefolder.Text = tmp; tmp = ""; ini.GetValue("TargetFolder", ref tmp); tbTargetFolder.Text = tmp; tmp = ""; ini.GetValue("Interval", ref tmp); if (!tmp.Trim().Equals("")) { cbInterval.Text = tmp; } tmp = ""; ini.GetValue("ServerIp", ref tmp); tbServerIp.Text = tmp; tmp = ""; ini.GetValue("Filter", ref tmp); tbFilter.Text = tmp; tmp = ""; ini.GetValue("Speed", ref tmp); if (!tmp.Trim().Equals("")) { tbSpeed.Text = tmp; } tmp = ""; ini.GetValue("FilesAtOnce", ref tmp); if (!tmp.Trim().Equals("")) { tbFilesAtOnce.Text = tmp; } tmp = ""; ini.GetValue("SkipLatency", ref tmp); if (!tmp.Trim().Equals("")) { tbSkipLatency.Text = tmp; } tmp = ""; ini.GetValue("FileCopyLatency", ref tmp); if (!tmp.Trim().Equals("")) { tbFileCopyLatency.Text = tmp; } tmp = ""; ini.GetValue("LocalServerIp", ref tmp); if (!tmp.Trim().Equals("")) { foreach (String ip in cbLocalIpAddresses.Items) { if (ip.Equals(tmp.Trim())) { cbLocalIpAddresses.Text = tmp; } } } tmp = ""; ini.GetValue("LocalServerPort", ref tmp); if (!tmp.Trim().Equals("")) { tbLocalPort.Text = tmp; } tmp = ""; ini.GetValue("ServerPort", ref tmp); tbServerPort.Text = tmp; if (cbLocalIpAddresses.Items.Count > 0 && cbLocalIpAddresses.SelectedIndex == -1) { cbLocalIpAddresses.SelectedIndex = 0; } // If we don't have a local server address, bail: if (!IPAddress.TryParse(cbLocalIpAddresses.Text.Trim(), out localServerAddress)) { return; } // If we don't have a port, bail: if (!UInt16.TryParse(tbLocalPort.Text.Trim(), out localServerPort)) { return; } // Start the local server: if (!localServer.Start(localServerPort, localServerAddress, ref errMsg)) { lblStatus.Text = errMsg; MessageBox.Show(errMsg, "Could not listen for incomming files!", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { lblStatus.Text = "Listening for incomming files on " + localServerAddress.ToString() + ":" + localServerPort.ToString(); } } this.Show(); }
private void btSave_Click(object sender, EventArgs e) { InIFileControler ini = new InIFileControler(iniFilePath); String errMsg = ""; try { localServer.Close(); } catch (Exception) { } lblStatus.Text = "Saving configuration..."; TcpComm.Client testClient = new TcpComm.Client(delegate(string tag, byte[] buffer, int numBytesContained, string sessionId) { // Do nothing here! }, tbUniqueId.Text.Trim() != "" ? tbUniqueId.Text.Trim() : "TransferSessionSettings"); lblStatus.Text = "Attempting to verify the configured server..."; if (!testClient.Connect(ushort.Parse(tbServerPort.Text.Trim()), tbServerIp.Text.Trim(), ref errMsg)) { lblStatus.Text = "Could not connect to the configured server."; } testClient.Close(); if (!Directory.Exists(tbSourcefolder.Text.Trim())) { MessageBox.Show("The source folder you entered doesn't exist. Please check your configuration and try again.", "Problem saving configuration!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (tbTargetFolder.Text.Trim().Equals("")) { MessageBox.Show("A target folder is required.", "Problem saving configuration!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!ini.ClearINI(ref errMsg)) { MessageBox.Show("Could not access the ini file: " + iniFilePath + Environment.NewLine + Environment.NewLine + ". The error returned is: " + errMsg, "Problem saving configuration!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ini.WriteEntry("ID", tbUniqueId.Text.Trim()); ini.WriteEntry("SourceFolder", tbSourcefolder.Text.Trim()); ini.WriteEntry("TargetFolder", tbTargetFolder.Text.Trim()); ini.WriteEntry("Interval", cbInterval.Text); ini.WriteEntry("ServerIp", tbServerIp.Text.Trim()); ini.WriteEntry("ServerPort", tbServerPort.Text.Trim()); ini.WriteEntry("Filter", tbFilter.Text.Trim()); ini.WriteEntry("Speed", tbSpeed.Text.Trim()); ini.WriteEntry("FilesAtOnce", tbFilesAtOnce.Text.Trim()); ini.WriteEntry("SkipLatency", tbSkipLatency.Text.Trim()); ini.WriteEntry("FileCopyLatency", tbFileCopyLatency.Text.Trim()); ini.WriteEntry("LocalServerIp", cbLocalIpAddresses.Text.Trim()); ini.WriteEntry("LocalServerPort", tbLocalPort.Text.Trim()); if (!UInt16.TryParse(tbLocalPort.Text.Trim(), out localServerPort)) { MessageBox.Show("There's a problem with the port you configured for incomming files! Please have a look and try again...", "Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (cbLocalIpAddresses.SelectedIndex > -1) { if (!IPAddress.TryParse(cbLocalIpAddresses.Items[cbLocalIpAddresses.SelectedIndex].ToString(), out localServerAddress)) { MessageBox.Show("There's a problem with the IP Address you have configured for incomming files! Please have a look and try again...", "Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // Start the local server: if (!localServer.Start(localServerPort, localServerAddress, ref errMsg)) { lblStatus.Text = errMsg; MessageBox.Show(errMsg, "Could not listen for incomming files!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } lblStatus.Text = "Saved: Listening for incomming files on " + localServerAddress.ToString() + ":" + localServerPort.ToString(); }