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 các bạn viết xong phần mềm trên Winform, các bạn cần phải đóng gói phần mềm thành file cài đặt.

Bạn đang xem: đóng gói phần mềm bằng installshield

Cách đóng gói phần mềm c# visual studio 2019

Hướng dẫn đóng gói phần mềm Visual Studio 2019 để mang đi cài các máy khác, đóng gói phần mềm thành file cài đặt setup.exe với đầy đủ thông tin về phần …

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 các bạn đóng gói ứng dụng tool nhỏ cài đặt sử dụng script PowerShell.

Lợi ích của việc đóng gói này là file cài đặt của bạn khoảng 300MB (nếu bạn nào code bằng Devexpress thì sẽ thấy dung lượng rất nhiều).

Xem thêm:

Cách xuất file exe trong visual studio

Cách 1: Các bước tạo một chương trình setup bằng Setup Wizard B1. Trong Solution Explorer của VS.net , right click vào Solution. Trong menu popup, vào mục Add –> New project . Xuất hiện hộp thoại Add new project. (H1 và H2)

Hình 2 B2. – Bên trái là Project Types: Chọn Other Project Types –> Setup and Deployment – Bên phải là Templates: Chọn Setup Wizard. Đặt tên Project mới vào trường name Chọn đường dẫn cần lưu thư mục tại trường Location Sau khi click Ok, VS.net sẽ tự động hiển thị hộp thoại là Setup Wizard (1 of 5). Click chọn Next (H3)

Hình 4 B3. Hộp thoại thứ 2 là Setup Wizard (2 of 5) xuất hiện . (H4) Hộp thoại này cho phép chọn kiểu Project Trong trường hợp này chúng ta giả sử đóng gói ứng dụng Windows nên đánh dấu check vào radio button : Create a setup for a Windows application. Còn nếu là ứng dụng Web thì ta chọn radio button : Create a setup for a web application. Next tiếp :

Hình 5 B4. Hộp thoại thứ 3 là Setup Wizard (3 of 5) xuất hiện . (H5) Hộp thoại này chứa các loại output mà chúng ta có thể đưa vào file Setup XXX là tên project cần đóng gói . * Localized resources from XXX : * XML Serialization Assemblies from XXX : * Content Files from XXX : chứa toàn bộ file chứa trong project XXX , cái này không cần thiết phải chọn. * Primary output from XXX : chứa file exe và file dll của prject XXX , cái này tất nhiên phải chọn rồi. * Source files from XXX : chứa file source code của project cần đóng gói như file css , vb , cpp … * Debug Symbols from XXX : chứa một số file hỗ trợ debug của dự án . * Documentation Files XXX : (tài liệu XML của dự án, cái này liên quan đến các commnets viết theo qui tắc convention của .Net) . Có thể đưa vào hoặc không. * Built Output from …: tạo ra file MSI * Primary Output (chứa các DLL và EXE do Project phía trên tạo ra) – đương nhiên phải đưa vào Bạn muốn biết thêm thông tin thì có thể xem Description ở bên dưới của hộp thoại đó. Sau khi chọn xong nhấn Next. B5. Hộp thoại thứ 4 là Setup Wizard (4 of 5) xuất hiện (xem H6)

Hình 6 Hộp thoại cho phép chọn file đính kèm theo như file Help , ReadMe … Nếu muốn thêm file nào thì chọn Add à chọn đường dẫn của file đó. Nếu không thì chọn Next. B6. Hộp thoại thứ 5 xuất hiện Setup wizard (5 of 5) thông báo tóm tắt nội dung kết quả. (xem H8)

B7. Chọn Finish để kết thúc quá trình tạo file cài đặt . B8. Sau khi nhấn nút Finish trong Wizard cuối cùng thì màn hình (xem H8) hiện ra.

Hình 8: File System Designer Trong Solution Explorer xuất hiện thêm Project , người ta gọi là Setup Project . Tới đây có thể tạm thời gọi là xong , nhưng chúng ta phải qua một số bước cấu hình để tạo ra file setup đóng gói phần mềm theo ý mình .

Nhược điểm: Muốn cài đặt 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 các bạn

Và khi chạy Script cài đặt chúng ta sẽ thực hiện các bước công việc 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 chúng ta đã tải về

Bước 5: Chạy mở ứng dụng lên

Bước 6: Tạo shortcut phần mềm 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 cài đặt thành công:

Source code full script PowerShell setup:

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

Xuất file exe trong visual studio 2019

Tạo file .exe trong visual studio

Nguồn: https://httl.com.vn/

Chuyên mục:

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 *