Я пытаюсь создать автоматическое обновление для приложения, однако у меня возникают проблемы с частью обновления. В основном у меня есть служба Windows, которая периодически проверяет наличие обновлений, а когда находит и обновляет ее, запускает консольное приложение для обновления. Код для консольного приложения приведен ниже.Не удается переустановить службу
Проблема, с которой я столкнулась, заключается в том, что при удалении службы и замене файла, который управляет службой, я получаю исключение system.badimageformat. Несмотря на переустановку идентичного файла. Если я удалю и переустановлю файл без его загрузки и заменив его с FTP, нет проблемы, но как только я сменил файл, он начинает давать мне исключения. У кого-нибудь есть идеи о том, как я могу обойти эту ошибку. Я уверен, что это не проблема с 32-го и 64-разрядным версиями, что обычно вызывает эту ошибку.
static void Main(string[] args)
{
if (!System.Diagnostics.EventLog.SourceExists("OCR Updater"))
{
EventLog.CreateEventSource("OCR Updater", "Application");
}
ServiceController sc = new ServiceController("OCR Scheduler", ".");
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
}
ProcessStartInfo Uninstallpsi = new ProcessStartInfo();
Uninstallpsi.Verb = "runas";
Uninstallpsi.UseShellExecute = false;
Uninstallpsi.FileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "installutil.exe";
Uninstallpsi.Arguments = " /u " + "\"" + AppDomain.CurrentDomain.BaseDirectory.ToString() + "OCR_Scheduler_Service.exe\"";
Process.Start(Uninstallpsi);
Console.WriteLine("Sleeping Thread after uninstall");
System.Threading.Thread.Sleep(10000);
OCRUpdater program = new OCRUpdater();
List<string> Files = program.GetFiles();
foreach (string item in Files)
{
if (item.ToString() == "Sqlite" || item.ToString() == "License.xml")
{
continue;
}
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.mccinnovations.com/OCR_Scheduler/V2Updates/Files/" + item);
request.Method = WebRequestMethods.Ftp.DownloadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("ftp admin", "_Stingray_12");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string[] temp = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
reader.Close();
response.Close();
string DesktopFile = "";
try
{
DesktopFile = @"C:\Users\hnelson\Desktop\" + item;
if (File.Exists(DesktopFile))
{
File.Delete(DesktopFile);
}
File.WriteAllLines(DesktopFile, temp);
}
catch (Exception ex)
{
EventLog.WriteEntry("OCR Updater", "Error in file path" + DesktopFile + ex.Message);
continue;
}
try
{
File.Delete(@"C:\Program Files (x86)\MCCi\OCR Scheduler V2\" + item);
System.Threading.Thread.Sleep(2000);
File.Copy(DesktopFile, @"C:\Program Files (x86)\MCCi\OCR Scheduler V2\" + item, true);
File.Delete(DesktopFile);
EventLog.WriteEntry("OCR Updater", DesktopFile);
}
catch (Exception)
{
EventLog.WriteEntry("OCR Updater", DesktopFile);
EventLog.WriteEntry("OCR Updater", "Error in file path " + @"C:\Program Files (x86)\MCCi\OCR Scheduler V2\" + item);
continue;
}
}
try
{
System.Threading.Thread.Sleep(5000);
ProcessStartInfo psi = new ProcessStartInfo();
psi.Verb = "runas";
psi.UseShellExecute = false;
psi.FileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "installutil.exe";
psi.Arguments = " " + "\"" + AppDomain.CurrentDomain.BaseDirectory.ToString() + "OCR_Scheduler_Service.exe\"";
Process.Start(psi);
}
catch (Exception ex)
{
EventLog.WriteEntry("OCR Updater", "Could not reinstall service" + ex.Message + ex.InnerException);
}
System.Threading.Thread.Sleep(10000);
Console.WriteLine("Finished resinstalling the service.");
try
{
string[] serviceStartArgs = { "true" };
sc.Start(serviceStartArgs);
sc.WaitForStatus(ServiceControllerStatus.Running);
}
catch (Exception ex)
{
EventLog.WriteEntry("OCR Updater", "Could not start the service after install" + " " + ex.Message + ex.InnerException);
}
}
public List<string> GetFiles()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.mccinnovations.com/OCR_Scheduler/V2Updates/Files/");
request.Method = WebRequestMethods.Ftp.ListDirectory;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("ftp admin", "_Stingray_12");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
List<string> Files = new List<string>();
while (reader.EndOfStream == false)
{
Files.Add(reader.ReadLine());
}
reader.Close();
response.Close();
return Files;
}
}
}
Это была настоящая проблема, я потратил буквально дни, инвестируя разрешения на сервер Windows, настраивая визуальные студийные проекты и 2-е угадывание каждой DLL в моем проекте. Большое спасибо, что-то очень простое, но я бы никогда не видел его. Не программировали достаточно долго, чтобы узнать разницу. –