WPF 打包

打包为单个exe文件直接运行

- - - 版本
.NET 8
  • 新建WPF项目

  • 右键 - 发布

  • 选择发布文件夹

  • 选择发布文件夹

  • 选择发布文件夹

  • 配置

  • 配置,保存

  • 发布

WPF 打包为exe安装程序

示例

  • 实现思路

    • 引导项目中嵌入其它项目可运行目录的zip
    • 引导项目中解压zip文件到指定文件夹
    • 是否创建快捷方式
  • 将 WPF 项目的 Debug 文件夹打包为zip

  • 按照上述 新建一个安装引导项目, 打包为单个exe文件直接运行

    • 引入 Debug.zip 文件
      • 右键该文件
    • 安装引导自定义 (例)
      • 用户是否同意
      • 安装目录
      • 是否创建快捷方式
  • 解压方法

csharp 复制代码
/// <summary>
/// 解压方法
/// </summary>
/// <param name="zipFilePath">zip文件目录  D:\\123.zip</param>
/// <param name="destinationFolder">解压目录  D:\\123</param>
public static void UnzipFile(string zipFilePath, string destinationFolder)
{
    Directory.CreateDirectory(destinationFolder);
    using (var zipToOpen = new FileStream(zipFilePath, FileMode.Open))
    {
        using (var archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
        {
            foreach (var entry in archive.Entries)
            {
                if (entry.Length == 0) continue;
                var destFileName = Path.Combine(destinationFolder, entry.FullName);
                var destinationDirectory = Path.GetDirectoryName(destFileName);
                Directory.CreateDirectory(destinationDirectory);
                using (var entryStream = entry.Open())
                using (var fileStream = new FileStream(destFileName, FileMode.Create))
                {
                    entryStream.CopyTo(fileStream);
                }
            }
        }
    }
} 
  • 创建快捷方式

csharp 复制代码
string exePath = @"C:\路径\xxxx.exe";
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string shortcutPath = Path.Combine(desktopPath, "xxxx.lnk");
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
shortcut.Description = "描述";
shortcut.TargetPath = exePath; 
shortcut.WorkingDirectory = Path.GetDirectoryName(exePath);
shortcut.Save();
相关推荐
吾与谁归in3 小时前
C#实现本地Deepseek模型及其他模型的对话
人工智能·c#·wpf·deepseek
lisenustc1 天前
WPF学习之Prism(二)
学习·wpf
百里与司空3 天前
WPF基本布局基础
学习·wpf
qq_382391333 天前
WPF框架学习
学习·wpf·1024程序员节
CE贝多芬4 天前
WPF的页面设计和实用功能实现
c#·wpf
酷炫码神4 天前
WPF布局控件
wpf
code_shenbing4 天前
WPF 实现虚拟键盘
c#·wpf
code_shenbing5 天前
WPF实现打印机控制及打印
wpf
界面开发小八哥5 天前
界面组件DevExpress WPF中文教程:Grid - 如何显示和隐藏列?
wpf·界面控件·devexpress·ui开发·.net9
虚假程序设计5 天前
python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML
python·ui·wpf