270 lines
14 KiB
C#
270 lines
14 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
|
|
namespace LocalSynchronizationSetup
|
|
{
|
|
internal class Synchronization
|
|
{
|
|
private string strPraefix = "Sync_";
|
|
private string strBatchSuffix = ".bat";
|
|
private string strShortcutSuffix = ".lnk";
|
|
private string strModul3D = "Modul_3D";
|
|
private string strNxEnvironment = "NX-Environment";
|
|
private string strNxMachineryLibrary = "NX_Machinery_Library";
|
|
private string strNxPortal = "NX-Portal";
|
|
private string strSyncDirectory = "C:\\Data\\NX";
|
|
private string strAppStartup;
|
|
|
|
public Synchronization(string strAppStartup)
|
|
{
|
|
this.strAppStartup = strAppStartup;
|
|
}
|
|
|
|
public Synchronization(string strPraefix, string strBatchSuffix, string strShortcutSuffix, string strModul3D, string strNxEnvironment, string strSyncDirectory, string strAppStartup)
|
|
{
|
|
this.strPraefix = strPraefix;
|
|
this.strBatchSuffix = strBatchSuffix;
|
|
this.strShortcutSuffix = strShortcutSuffix;
|
|
this.strModul3D = strModul3D;
|
|
this.strNxEnvironment = strNxEnvironment;
|
|
this.strSyncDirectory = strSyncDirectory;
|
|
this.strAppStartup = strAppStartup;
|
|
}
|
|
|
|
public bool StrAppStartupContainsFolderNx(bool showMessages, string strUsage)
|
|
{
|
|
if (Directory.Exists(this.strAppStartup + "\\" + this.strNxEnvironment) && Directory.Exists(this.strAppStartup + "\\" + this.strNxPortal))
|
|
{
|
|
return true;
|
|
}
|
|
if (showMessages)
|
|
{
|
|
int num = (int)MessageBox.Show("With the current settings the synchronization can not be created or removed. Could not find Folder NX in Startup Directory.\r\n" + "The current Startup Directory is: \"" + this.strAppStartup + "\"\r\n\r\n" + strUsage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool CreateRemoveSynchronisation(bool createNxEnvironmentSync, bool removeNxEnvironmentSync, bool createModul3DSync, bool removeModul3DSync, bool createNxMachineryLibrarySync, bool removeNxMachineryLibrarySync, bool createNxPortalSync, bool removeNxPortalSync, bool show_messages, out string summary)
|
|
{
|
|
summary = "Summary:\n";
|
|
|
|
try
|
|
{
|
|
if (createNxEnvironmentSync)
|
|
{
|
|
if (!Directory.Exists(this.strSyncDirectory))
|
|
{
|
|
Directory.CreateDirectory(this.strSyncDirectory);
|
|
if (!Directory.Exists(this.strSyncDirectory))
|
|
{
|
|
if (show_messages)
|
|
{
|
|
int num = (int)MessageBox.Show("Unable to create synchronization directory: \n" + this.strSyncDirectory, "Create Synchronization Directory", MessageBoxButtons.OK, MessageBoxIcon.Hand);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
if (!File.Exists(this.strSyncDirectory + "\\robocopy.exe"))
|
|
{
|
|
File.Copy(Application.StartupPath.ToString() + "\\robocopy.exe", this.strSyncDirectory + "\\robocopy.exe", false);
|
|
summary = !this.CreateSyncProcess(this.strNxEnvironment, show_messages) ? summary + "\nUnable to create synchronisation process for " + this.strNxEnvironment : summary + "\nSynchronisation process created for " + this.strNxEnvironment;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
summary = !removeNxEnvironmentSync ? summary + "\nNo synchronization changes for " + this.strNxEnvironment : (!this.RemoveSyncProcess(this.strNxEnvironment) ? summary + "\nUnable to remove synchronisation process for " + this.strNxEnvironment : summary + "\nSynchronisation process removed for " + this.strNxEnvironment);
|
|
}
|
|
if (createModul3DSync)
|
|
{
|
|
if (!Directory.Exists(this.strSyncDirectory))
|
|
{
|
|
Directory.CreateDirectory(this.strSyncDirectory);
|
|
if (!Directory.Exists(this.strSyncDirectory))
|
|
{
|
|
if (show_messages)
|
|
{
|
|
int num = (int)MessageBox.Show("Unable to create synchronization directory: \n" + this.strSyncDirectory, "Create Synchronization Directory", MessageBoxButtons.OK, MessageBoxIcon.Hand);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
if (!File.Exists(this.strSyncDirectory + "\\robocopy.exe"))
|
|
{
|
|
File.Copy(this.strAppStartup + "\\robocopy.exe", this.strSyncDirectory + "\\robocopy.exe", false);
|
|
}
|
|
summary = !this.CreateSyncProcess(this.strModul3D, show_messages) ? summary + "\nUnable to create synchronisation process for " + this.strModul3D : summary + "\nSynchronisation process created for " + this.strModul3D;
|
|
}
|
|
else
|
|
{
|
|
summary = !removeModul3DSync ? summary + "\nNo synchronization changes for " + this.strModul3D : (!this.RemoveSyncProcess(this.strModul3D) ? summary + "\nUnable to remove synchronisation process for " + this.strModul3D : summary + "\nSynchronisation process removed for " + this.strModul3D);
|
|
}
|
|
if (createNxMachineryLibrarySync)
|
|
{
|
|
if (!Directory.Exists(this.strSyncDirectory))
|
|
{
|
|
Directory.CreateDirectory(this.strSyncDirectory);
|
|
if (!Directory.Exists(this.strSyncDirectory))
|
|
{
|
|
if (show_messages)
|
|
{
|
|
int num = (int)MessageBox.Show("Unable to create synchronization directory: \n" + this.strSyncDirectory, "Create Synchronization Directory", MessageBoxButtons.OK, MessageBoxIcon.Hand);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
if (!File.Exists(this.strSyncDirectory + "\\robocopy.exe"))
|
|
{
|
|
File.Copy(Application.StartupPath.ToString() + "\\robocopy.exe", this.strSyncDirectory + "\\robocopy.exe", false);
|
|
}
|
|
summary = !this.CreateSyncProcess(this.strNxMachineryLibrary, show_messages) ? summary + "\nUnable to create synchronisation process for " + this.strNxMachineryLibrary : summary + "\nSynchronisation process created for " + this.strNxMachineryLibrary;
|
|
}
|
|
else
|
|
{
|
|
summary = !removeNxMachineryLibrarySync ? summary + "\nNo synchronization changes for " + this.strNxMachineryLibrary : (!this.RemoveSyncProcess(this.strNxMachineryLibrary) ? summary + "\nUnable to remove synchronisation process for " + this.strNxMachineryLibrary : summary + "\nSynchronisation process removed for " + this.strNxMachineryLibrary);
|
|
}
|
|
if (createNxPortalSync)
|
|
{
|
|
if (!Directory.Exists(this.strSyncDirectory))
|
|
{
|
|
Directory.CreateDirectory(this.strSyncDirectory);
|
|
if (!Directory.Exists(this.strSyncDirectory))
|
|
{
|
|
if (show_messages)
|
|
{
|
|
int num = (int)MessageBox.Show("Unable to create synchronization directory: \n" + this.strSyncDirectory, "Create Synchronization Directory", MessageBoxButtons.OK, MessageBoxIcon.Hand);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
if (!File.Exists(this.strSyncDirectory + "\\robocopy.exe"))
|
|
{
|
|
File.Copy(Application.StartupPath.ToString() + "\\robocopy.exe", this.strSyncDirectory + "\\robocopy.exe", false);
|
|
}
|
|
summary = !this.CreateSyncProcess(this.strNxPortal, show_messages) ? summary + "\nUnable to create synchronisation process for " + this.strNxPortal : summary + "\nSynchronisation process created for " + this.strNxPortal;
|
|
}
|
|
else
|
|
{
|
|
summary = !removeNxPortalSync ? summary + "\nNo synchronization changes for " + this.strNxPortal : (!this.RemoveSyncProcess(this.strNxPortal) ? summary + "\nUnable to remove synchronisation process for " + this.strNxPortal : summary + "\nSynchronisation process removed for " + this.strNxPortal);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
int num = (int)MessageBox.Show(ex.ToString());
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public string ShortcutFileName(string SyncName)
|
|
{
|
|
return new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Startup)).ToString() + "\\" + this.strPraefix + SyncName + this.strShortcutSuffix;
|
|
}
|
|
|
|
public string BatchFileName(string SyncName)
|
|
{
|
|
return this.strSyncDirectory + "\\" + this.strPraefix + SyncName + this.strBatchSuffix;
|
|
}
|
|
|
|
private bool CreateSyncProcess(string SyncName, bool show_messages)
|
|
{
|
|
if (!Directory.Exists(this.strAppStartup + "\\" + SyncName))
|
|
{
|
|
if (show_messages)
|
|
{
|
|
int num = (int)MessageBox.Show("Unable to create synchronization process.\n\nCannot find directory \"" + SyncName + "\" in application startup directory.", "Create Synchronization Process", MessageBoxButtons.OK, MessageBoxIcon.Hand);
|
|
}
|
|
return false;
|
|
}
|
|
return this.CreateSyncBatch(SyncName) && this.CreateSyncShortcut(SyncName);
|
|
}
|
|
|
|
private bool RemoveSyncProcess(string SyncName)
|
|
{
|
|
return this.RemoveSyncShortcut(SyncName) && this.RemoveSyncBatch(SyncName);
|
|
}
|
|
|
|
private bool CreateSyncBatch(string SyncName)
|
|
{
|
|
if (Directory.Exists(this.strSyncDirectory))
|
|
{
|
|
if (File.Exists(this.BatchFileName(SyncName)))
|
|
{
|
|
try
|
|
{
|
|
File.Delete(this.BatchFileName(SyncName));
|
|
}
|
|
catch
|
|
{
|
|
//Tu nichts
|
|
}
|
|
}
|
|
if (!File.Exists(this.BatchFileName(SyncName)))
|
|
{
|
|
FileStream fileStream = File.Create(this.BatchFileName(SyncName));
|
|
StreamWriter streamWriter = new StreamWriter((Stream)fileStream);
|
|
streamWriter.WriteLine("ECHO OFF");
|
|
streamWriter.WriteLine("TITLE Synchronize " + SyncName);
|
|
streamWriter.WriteLine("ECHO.");
|
|
streamWriter.WriteLine("ECHO +++ Synchronize " + SyncName);
|
|
streamWriter.WriteLine("ECHO +++ File Created by LocalSynchronization Setup");
|
|
streamWriter.WriteLine("ECHO +++ written by Eugen Hoeglinger, 2010/03/26");
|
|
streamWriter.WriteLine("ECHO -----------------------------------------");
|
|
streamWriter.WriteLine("ECHO.");
|
|
streamWriter.WriteLine("SET ROBOCOPY_PATH=%~dp0");
|
|
streamWriter.WriteLine("SET ROBOCOPY_PATH=%ROBOCOPY_PATH:~0,-1%");
|
|
streamWriter.WriteLine("SET SYNC_SOURCE=%NX_ONLINE%\\NX\\" + SyncName);
|
|
streamWriter.WriteLine("SET SYNC_DESTINATION=C:\\Data\\NX\\" + SyncName);
|
|
streamWriter.WriteLine("ECHO Start Synchronization... ");
|
|
streamWriter.WriteLine("ECHO SOURCE: %SYNC_SOURCE%");
|
|
streamWriter.WriteLine("ECHO DESTINATION: %SYNC_DESTINATION%");
|
|
streamWriter.WriteLine("ECHO.");
|
|
streamWriter.WriteLine("\"%ROBOCOPY_PATH%\\robocopy.exe\" \"%SYNC_SOURCE%\" \"%SYNC_DESTINATION%\" /MIR /R:3 /W:30 /Log:\"" + this.strSyncDirectory + "\\Sync_NX_" + SyncName + "_SyncLog.txt\" /TEE");
|
|
streamWriter.WriteLine("exit");
|
|
streamWriter.Flush();
|
|
streamWriter.Close();
|
|
fileStream.Close();
|
|
return File.Exists(this.BatchFileName(SyncName));
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private bool CreateSyncShortcut(string SyncName)
|
|
{
|
|
if (File.Exists(this.ShortcutFileName(SyncName)))
|
|
{
|
|
File.Delete(this.ShortcutFileName(SyncName));
|
|
}
|
|
if (File.Exists(this.ShortcutFileName(SyncName)))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
string text = String.Concat(strPraefix, SyncName);
|
|
string source = String.Concat(strSyncDirectory, "\\", strPraefix, SyncName, strBatchSuffix);
|
|
string path = this.ShortcutFileName(SyncName).Remove(this.ShortcutFileName(SyncName).LastIndexOf("\\") + 1);
|
|
Shortcut.Create(text, source, path);
|
|
|
|
return File.Exists(this.ShortcutFileName(SyncName));
|
|
}
|
|
|
|
private bool RemoveSyncShortcut(string SyncName)
|
|
{
|
|
if (File.Exists(this.ShortcutFileName(SyncName)))
|
|
{
|
|
File.Delete(this.ShortcutFileName(SyncName));
|
|
}
|
|
return !File.Exists(this.ShortcutFileName(SyncName));
|
|
}
|
|
|
|
private bool RemoveSyncBatch(string SyncName)
|
|
{
|
|
if (File.Exists(this.BatchFileName(SyncName)))
|
|
{
|
|
File.Delete(this.BatchFileName(SyncName));
|
|
}
|
|
return !File.Exists(this.BatchFileName(SyncName));
|
|
}
|
|
}
|
|
}
|