Cách đóng gói code project thành file exe bằng ms visual studio

Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn đóng gói phần mềm thành file cài đặt installer sử dụng script PowerShell.Bạn đang xem: đóng gói phần mềm thành file exe

Sau khi những bạn viết xong ứng dụng trên Winform, những bạn cần phải đóng gói ứng dụng thành file thiết lập .
Bạn đang xem :

Bình thường, các bạn sẽ sử dụng phần mềm: Advance Installer, One Click Install hay Installshield…

Trong bài viết này mình sẽ hướng dẫn những bạn đóng gói ứng dụng tool nhỏ setup sử dụng script PowerShell .
Lợi ích của việc đóng gói này là file thiết lập của bạn khoảng chừng 300MB ( nếu bạn nào code bằng Devexpress thì sẽ thấy dung tích rất nhiều ) .
Xem thêm :
Nhược điểm : Muốn thiết lập phải có mạng internet .

Các bạn có thể mở chương trình Powershell ISE, để code script

*

Các bước đóng gói phần mềm SVG Collection của mình.

Bước 1 : Nén File ứng dụng ở thư mục Release lên hosting web của những bạn
Và khi chạy Script setup tất cả chúng ta sẽ thực thi những bước việc làm sau :
Bước 2 : Download file từ hosting name VD : https://onlineaz.vn/svg_csharp.rar

Bước 3: Giải nén file rar vào thư mục chúng ta chỉ định

Bước 4 : Xóa File rar tất cả chúng ta đã tải về
Bước 5 : Chạy mở ứng dụng lên
Bước 6 : Tạo shortcut ứng dụng trước Desktop .
Xem thêm :

Sau khi Viết Script PowerShell xong, chúng ta sẽ sử dụng PS2EXE => chuyển file script thành file EXE

Video demo ứng dụng tạo sau khi tạo file setup thành công xuất sắc :
Source code full script PowerShell setup :
:: InputEncoding = :: OutputEncoding = New-Object System. Text. UTF8Encoding USD host. UI.RawUI.WindowTitle = “ Cài đặt ứng dụng Powershell – https://onlineaz.vn”Add-Type – AssemblyName System. Windows. Forms USD browser = New-Object System. Windows. Forms. FolderBrowserDialog USD browser. Description = “ Bạn Hãy chọn thư mục setup ứng dụng ” USD null = USD browser. ShowDialog ( ) USD temp = USD browser. SelectedPathWrite-Output USD temp USD url = “ https://onlineaz.vn/svg_csharp.rar”$output = USD temp + “ svg_csharp. rar ” USD start_time = Get-Date # Import-Module BitsTransfer # Start-BitsTransfer – Source USD url – Destination USD output # OR # Start-BitsTransfer – Source USD url – Destination USD output – AsynchronousFunction Get-Webfile ( USD url ) { USD dest = USD output Write-Host “ Đang tải USD url ` n ” – ForegroundColor DarkGreen ; USD uri = New-Object “ System. Uri ” “ USD url ” USD request = :: Create ( USD uri ) USD request. set_Timeout ( 5000 ) USD response = USD request. GetResponse ( ) USD totalLength = :: Floor ( USD response. get_ContentLength ( ) / 1024 ) USD length = USD response. get_ContentLength ( ) USD responseStream = USD response. GetResponseStream ( ) USD destStream = New-Object – TypeName System. IO.FileStream – ArgumentList USD dest, Create USD buffer = New-Object byte 10KB USD count = USD responseStream. Read ( USD buffer, 0, USD buffer.length ) USD downloadedBytes = USD count while ( USD count – gt 0 ) { :: CursorLeft = 0 :: Write ( “ Đang tải ứng dụng { 0 } K of { 1 } K ( { 2 } % ) ”, :: Floor ( USD downloadedBytes / 1024 ), USD totalLength, :: Round ( ( USD downloadedBytes / USD length ) * 100,0 ) ) USD destStream. Write ( USD buffer, 0, USD count ) USD count = USD responseStream. Read ( USD buffer, 0, USD buffer.length ) USD downloadedBytes + = USD count } Write-Host “ ” Write-Host “ ` nDownload of ` ” USD dest ` ” finished. ” – ForegroundColor DarkGreen ; USD destStream. Flush ( ) USD destStream. Close ( ) USD destStream. Dispose ( ) USD responseStream. Dispose ( ) } Get-Webfile USD urlWrite-Output “ Time taken : USD ( ( Get-Date ). Subtract ( USD start_time ). Seconds ) second ( s ) ” Set-Location USD tempFunction Extract-WinRarFiles { Param ( USD UnRarExePath = “ USD env : ProgramFilesWinRARUnRAR. exe ”, USD UnRarSourcePath, USD UnRarTargetPath, USD OpenTargetLocation, $ DeleteSourceRarFiles ) Begin { USD NewLine = “ ` r ` n ” USD RarFilePaths = ( Get-ChildItem – Path USD UnRarSourcePath – Recurse | Where-Object – FilterScript { USD _. extension – eq ‘. rar ’ } ). FullName USD RarFileSourceCount = USD RarFilePaths. Count } Process { USD NewLine Write-Output – Verbose “ Total RAR File Count : USD RarFileSourceCount ” USD NewLine Write-Output – Verbose “ Beginning extraction, please wait … ” Start-Sleep – Seconds 2 Foreach ( USD FilePath in USD RarFilePaths ) { và USD UnRarExePath x – y USD FilePath USD UnRarTargetPath } USD RarFileTargetCount = ( Get-ChildItem – Path USD UnRarTargetPath ). Count If ( $ RarFileTargetCount – eq USD RarFileSourceCount ) { Clear-Host USD NewLine Write-Output – Verbose “ USD RarFileTargetCount RAR files have been extracted ” USD NewLine } Else { USD NewLine Write-Warning – Message “ USD RarFileTargetCount out of USD RarFileSourceCount have been extracted ” USD NewLine } } End { Switch ( USD PSBoundParameters. Keys ) { { USD _ – contains ‘ OpenTargetLocation ’ } { USD NewLine Write-Output – Verbose ‘ Opening RAR target location … ’ Start-Sleep – Seconds 5 Invoke-Item – Path USD UnRarTargetPath } { USD _ – contains ‘ DeleteSourceRarFiles ’ } { USD NewLine Write-Output – Verbose ‘ Deleting source RAR files and the directory … ’ Start-Sleep – Seconds 5 Remove-Item – Path USD UnRarSourcePath – Recurse – Force } } } } Set-Location USD temp USD appPath = USD temp + “ SGV Collection. exe ” Extract-WinRarFiles – UnRarSourcePath USD output – UnRarTargetPath USD temp – DeleteSourceRarFiles Write-Output “ Time taken : USD ( ( Get-Date ). Subtract ( USD start_time ). Seconds ) second ( s ) ” Remove-Item *. tmp – recurse – force Start-Process – FilePath USD appPath USD WshShell = New-Object – comObject WScript. Shell USD Shortcut = USD WshShell. CreateShortcut ( “ USD HomeDesktopSGV Collection. lnk ” ) USD Shortcut. TargetPath = USD appPath USD Shortcut. Save ( ) write-host “ Setup finish … ” write-host “ Press any key to exist … ” # :: ReadKey ( USD true ) Sau khi tạo script xong để biên dịch File script thành File EXE những bạn vào đường dẫn github PS2EXE sau để xem cụ thể tạo file setup .
https://github.com/MScholtes/PS2EXE
Bước 1 : Các bạn mở chương trình PowerShell lên

Bước 2: Gõ lệnh tải PS2EXE về

PS C : > Install-Module ps2exeBước 3 : Lệnh đóng gói, những bạn chỉ chọn đường dẫn file script và file xuất exe
Invoke-ps2exe. source. ps1. arget. exeKhi build File Exe, nếu những bạn muốn chỉnh sửa những tham số Assembly hay icon của ứng dụng, khi build những bạn làm theo cú pháp bên dưới :
Chuyên mục :

Source: https://expgg.vn
Category: Thông tin

Total
0
Shares
Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Previous Post

Cách chèn logo vào ảnh hàng loạt bằng Photoshop

Next Post

[Tips] Cách dùng điện thoại điều khiển máy tính từ xa với Vectir

Related Posts