Automatic update. Check the version number of the external program. Administrator permission. Multiple startup prohibited. code obfuscation, version number permission

Source: Internet
Author: User
Tags mailmessage smtpclient

Automatic update. Check the version number of the external program. Administrator permission. Multiple startup prohibited. code obfuscation, version number permission

Automatic update. Start program a. Check the version number of program B. Then upload and download the program on the server. To control the version number of other programs, check the version number of other programs. inconsistent versions will send text messages to developers.

ICSharpCode. SharpZipLib. dll decompression tool

Private string GetRequestDownFile (string url, string path) // download HTTP
{
Try
{
// Set parameters
HttpWebRequest request = WebRequest. Create (url) as HttpWebRequest;
// Send the request and obtain the response data
HttpWebResponse response = request. GetResponse () as HttpWebResponse;
// The Post request is not sent to the target webpage until the request. GetResponse () program starts.
Stream responseStream = response. GetResponseStream ();
// Create a local file write stream
Stream stream = new FileStream (path, FileMode. Create );
Byte [] bArr = new byte [1024];
Int size = responseStream. Read (bArr, 0, (int) bArr. Length );
While (size> 0)
{
Stream. Write (bArr, 0, size );
Size = responseStream. Read (bArr, 0, (int) bArr. Length );
}
Stream. Close ();
ResponseStream. Close ();
Return path;
}
Catch (Exception ef)
{

If (ef. Message = "failed to resolve this remote name: 'ekaipiao .gingkoo.com '")
{

}
If (ef. Message = "error returned by the remote server: (404) not found. ")
{

}
Return null;
}
}

URL corresponds to the server address. http://ekaipiao.gingkoo.com/clients/GingkooPrint (142.16.exe

The Client is followed by a relative path

Private void start ()
{
Process p = new Process (); // create a Process
P. StartInfo. FileName = Application. StartupPath + @ "\ Print.exe"; // set the process name
P. StartInfo. CreateNoWindow = true;
P. StartInfo. WindowStyle = ProcessWindowStyle. Normal;
P. Start ();
}

Program B starts program

 

File = GetRequestDownFile ("http://ekaipiao.gingkoo.com/clients/Service.ini", Application. StartupPath + @ "\ Service. ini ");

Download the server version number. If it is later than. download the latest version to overwrite

Guaranteed speed. The server stores the new version of ZIP files. The client downloads and decompress the files in the background to start.

FastZip fastZip = new FastZip (); // decompress the object

FastZip. ExtractZip (Application. StartupPath + @ "\ relase for Win7 or above.zip", Application. StartupPath, null );

 


// FastZip. ExtractZip (@ "D: \ test1.zip", @ "D: \ decompress directory", null); // decompress

// FastZip. CreateZip (@ "D: \ document .zip", @ "D: \ source directory", true, null); // Compression

Lossless

 

Start ();
Process [] proc = Process. GetProcesses ();
Foreach (var item in proc)
{
If (item. ProcessName. Contains ("AutoUpdate") // vshost
{
Item. Kill ();
}
}

 

Start the target program and you will be able to kill yourself.

 

How to detect external programs. Check the Registry. If it is a green software, read the desktop shortcut. If it is full of computers, search for it.

 

Private List <string> shorttopsean () // Desktop Search
{
List <string> data = new List <string> ();

String dir = System. Environment. GetFolderPath (System. Environment. SpecialFolder. Desktop );


DirectoryInfo dirInfo = new DirectoryInfo (dir );


FileInfo [] files = dirInfo. GetFiles ("*. lnk", SearchOption. TopDirectoryOnly); // the current directory
List <int> test = new List <int> ();
Foreach (FileInfo file in files)
{
Data. Add (file. DirectoryName + "\" + file. Name );
// Data. Add (AppDomain. CurrentDomain. SetupInformation. ApplicationBase + file );
}

Return data;


}

All shortcuts on the computer will be found. The EXE on the desktop is actually lnk

Private string VersionSean (string item) // check the version number
{
IWshShortcut _ shortcut = null;
IWshShell_Class shell = new IWshShell_Class ();
If (System. IO. File. Exists (item) = true)
_ Shortcut = shell. CreateShortcut (item) as IWshShortcut; // obtain the physical path through shortcuts

Var path1 = _ shortcut. TargetPath;

FileVersionInfo. GetVersionInfo (Path. Combine (path1 ));
FileVersionInfo myFileVersionInfo = FileVersionInfo. GetVersionInfo (path1); // obtain the version number through the physical path

Return myFileVersionInfo. FileVersion;

}

Private string WindowsVersionSean ()
{
String windowsversion = null;
OperatingSystem OS = Environment. OSVersion;
Switch (OS. Platform)
{
Case PlatformID. Win32Windows:
Switch (OS. Version. Minor)
{
Case 0:
Windowsversion = "Windows 95 ";
Break;
Case 10:
If (OS. Version. Revision. ToString () = "2222A ")
Windowsversion = "Windows 98 Second Edition ";
Else
Windowsversion = "Windows 98 ";
Break;
Case 90:
Windowsversion = "Windows Me ";
Break;
}
Break;
Case PlatformID. Win32NT:
Switch (OS. Version. Major)
{
Case 3:
Windowsversion = "Windows NT 3.51 ";
Break;
Case 4:
Windowsversion = "Windows NT 4.0 ";
Break;
Case 5:
Switch (OS. Version. Minor)
{
Case 0:
Windowsversion = "Windows 200 ";
Break;
Case 1:
Windowsversion = "Windows XP ";
Break;
Case 2:
Windowsversion = "Windows 2003 ";
Break;
}
Break;
Case 6:
Switch (OS. Version. Minor)
{
Case 0:
Windowsversion = "Windows Vista ";
Break;
Case 1:
Windowsversion = "Windows 7 ";
Break;
}
Break;
}
Break;
}
Return windowsversion;
} // Check the Win version

Why check the Win version number. This is the auto update part, because WIN7 and xp cannot use the same source code.

 

Var a = shorttopsean ();

Foreach (string item in)
{
If (item. Contains ("VAT invoice tax control invoicing software (tax control disk version). lnk") // baiwang
{
Revoke Wang = VersionSean (item );
}
If (item. Contains ("invoicing software. lnk") // airline mail
{
Hangxin = VersionSean (item );
}
}

 

In this way, you can read the version number of the external program.

 

[DllImport ("kernel32")]
Public static extern long WritePrivateProfileString (string section, string key, string val, string filePath );
[DllImport ("kernel32")]
Public static extern int GetPrivateProfileString (string section, string key, string def, StringBuilder retVal, int size, string filePath );

 

GetPrivateProfileString ("VersionData", "Key", "", temp, 500, file); // read

 

WritePrivateProfileString ("VersionData", "Key", temp. ToString (), Application. StartupPath + @ "\ Client. ini"); // write

 

This method can be used to operate the INI file.

Why? Because the version number is in the server's INI

If the version number of the external program on the client is detected to be inconsistent with the version number stored on the server, send a text message and send an email to inform our developers of the external program upgrade. See if there is any impact ~

 

Public static string GetHtmlFromUrl (string url)
{
String strRet = null;
If (url = null | url. Trim (). ToString () = "")
{
Return strRet;
}
String targeturl = url. Trim (). ToString ();
Try
{
HttpWebRequest hr = (HttpWebRequest) WebRequest. Create (targeturl );
Hr. UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1 )";
Hr. Method = "GET ";
Hr. Timeout = 30*60*1000;
WebResponse hs = hr. GetResponse ();
Stream sr = hs. GetResponseStream ();
StreamReader ser = new StreamReader (sr, Encoding. Default );
StrRet = ser. ReadToEnd ();
}
Catch (Exception ex)
{
StrRet = null;
}
Return strRet;
}

Call the SMS Interface

You can send five text messages for free ~

String name = GetHtmlFromUrl ("http://gbk.sms.webchinese.cn /? Uid = Account & Key = Key & smsMob = recipient & smsText = content ");

 

Public bool SendEmail (string StrTo, string strFrom, string StrBody, string strSubject, string name, string pwd, string path)
{
# Region check email category
String sHos = "smtp.qq.com"; // obtain your smtp host based on the email address
If (name. Contains ("@ 163 "))
{
SHos = "smtp.163.com ";
}
Else if (name. Contains ("@ 126 "))
{
SHos = "smtp.126.com ";
}
Else if (name. Contains ("@ qq "))
{
SHos = "smtp.qq.com ";
}
Else if (name. Contains ("@ sohu "))
{
SHos = "smtp.sohu.com ";
}
Else if (name. Contains ("@ sina "))
{
SHos = "smtp.sina.com ";
}
Else if (name. Contains ("@ tom "))
{
SHos = "smtp.tom.com.cn ";
}
Else if (name. Contains ("@ foxmail "))
{
SHos = "smtp.foxmail.com ";
}
Else if (name. Contains ("@ 139 "))
{
SHos = "smtp.139.com ";
}
# Endregion

// Set the recipient's address
System. Net. Mail. MailAddress fromEmail = new System. Net. Mail. MailAddress (strFrom );
System. Net. Mail. MailAddress toEmail = new System. Net. Mail. MailAddress (StrTo );

// Create an email object
System. Net. Mail. MailMessage mail = new System. Net. Mail. MailMessage (fromEmail, toEmail );

Mail. Body = StrBody; // mail content
Mail. Subject = strSubject; // mail title
Mail. IsBodyHtml = true; // whether the email contains the html Format
Mail. BodyEncoding = Encoding. UTF8; // Encoding method of the email content
Mail. Priority = System. Net. Mail. MailPriority. Normal; // mail Priority

// Postman
System. Net. Mail. SmtpClient smtpClient = new System. Net. Mail. SmtpClient ();
SmtpClient. Host = sHos; // set it to the smtp Host name or IP address of the sender.
SmtpClient. Port = 25; // default Port number
SmtpClient. usedefadefacredentials = false; // do not use the default creden of the current user for authentication. Use Credentials for authentication.
SmtpClient. DeliveryMethod = System. Net. Mail. SmtpDeliveryMethod. Network;
SmtpClient. Credentials = new System. Net. NetworkCredential (name, pwd); // your username and password

// The following is the attachment part of the email, and the path is the client path of the attachment with the file name
If (path! = "")
{
If (File. Exists (path ))
{
// Set the attachment type to any file type
Attachment attachment = new Attachment (path, System. Net. Mime. MediaTypeNames. Application. Octet );
Mail. Attachments. Add (attachment );
}
}
Try
{
SmtpClient. Send (mail );
}
Catch (Exception e)
{
// String s = e. Message;
Return false;
}
Return true;
}

 

SendEmail ("recipient email", "sender email", "title", "content", "sender account", "sender password", "attachment ");

In this way, if the client software detects that the external program version is upgraded. It will cry and tell developers

 

Using System;
Using System. Diagnostics;
Using System. Reflection;
Using System. Windows. Forms;

Namespace AutoUpdate
{
Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main (string [] Args)
{
// Application. EnableVisualStyles ();
// Application. SetCompatibleTextRenderingDefault (false );
// Application. Run (new Update ());
Process instance = RunningInstance ();
If (instance = null)
{
// No instance is running
// Application. Run (new Login ());
System. Security. Principal. WindowsIdentity identity = System. Security. Principal. WindowsIdentity. GetCurrent ();
// Create a Windows user topic
Application. EnableVisualStyles ();

System. Security. Principal. WindowsPrincipal principal = new System. Security. Principal. WindowsPrincipal (identity );
// Determine whether the current logon user is an administrator
If (principal. IsInRole (System. Security. Principal. WindowsBuiltInRole. Administrator ))
{
// Directly run

Application. EnableVisualStyles ();
Application. Run (new Update ());
}
Else
{
// Create a startup object
System. Diagnostics. ProcessStartInfo startInfo = new System. Diagnostics. ProcessStartInfo ();
// Set the running file Z
StartInfo. FileName = System. Windows. Forms. Application. ExecutablePath;
// Set startup parameters
StartInfo. Arguments = String. Join ("", Args );
// Set the startup action to run as an administrator
StartInfo. Verb = "runas ";
// Enable UAC if it is not an administrator
System. Diagnostics. Process. Start (startInfo );
// Exit
System. Windows. Forms. Application. Exit ();
}
}
Else
{
// An instance is running.
HandleRunningInstance (instance );
}
}
Private static Process RunningInstance ()
{
Process current = Process. GetCurrentProcess ();
Process [] processes = Process. GetProcessesByName (current. ProcessName );
// Traverse the process list with the same name as the current process
Foreach (Process process in processes)
{
// If the instance already exists, the current process is ignored.
If (process. Id! = Current. Id)
{
// Ensure that the process to be opened comes from the same file path as the existing process
If (Assembly. GetExecutingAssembly (). Location. Replace ("/", "\") = current. MainModule. FileName)
{
// Return an existing process
Return process;

}
}
}
Return null;
}

Private static void HandleRunningInstance (Process instance)
{
// MessageBox. Show ("already running! "," Message ", MessageBoxButtons. OK, MessageBoxIcon. Information );
// ShowWindowAsync (instance. main1_whandle, 1); // call the api function and display the window normally
// SetForegroundWindow (instance. main1_whandle); // place the window at the frontend
}
}
}

 

Simple. The above Program contains the Administrator to start and prohibit multiple starts.

The impetuousness of accidental deletion and rewriting ~

 

Code obfuscation is not a concern. Microsoft releases DotNet REACTOR. EXE.

Open add main program EXE

File to add other programs

Anti ildasm is used to prevent Decompilation

 

Obfuscation is code obfuscation

 

String encryption is string encryption

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.