【问题标题】:Extract files from within executable in c#从c#中的可执行文件中提取文件
【发布时间】:2014-09-13 11:23:42
【问题描述】:

所以我正在用 C# 编写一个程序,我需要将项目中的文件添加到用户系统上的文件夹中。

我在 Visual Studio 2013 的“解决方案资源管理器”中有一个文件夹,我希望这些文件能够以某种方式提取到用户系统上的目录中。

例如:

  1. 应用程序已执行。
  2. 应用程序从自身内部提取文件到 %appdata%/MyFiles/files

我是 C# 的新手并且正在学习,所以请告诉我这是否可行以及是否有更好的方法来解决这个问题。

提前致谢。

【问题讨论】:

标签: c# visual-studio-2013


【解决方案1】:

在 .Net 4.5 中使用一个名为 ZipFile 的新类

您可以提取文件。

using System;
    using System.IO;
    using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

【讨论】:

  • 我可能会感到困惑,但这只是从 .zip 档案中提取文件吗?
  • 向您展示了一个示例,该示例将在 CreateFromDirectory 的帮助下创建,并在 ExtractToDirectory 的帮助下提取。所以你需要的是调用 ExtractToDirectory 并给它一个正确的路径,你可以在 zipPath 变量中更改。
  • 那么上述方法会从可执行文件本身中提取文件吗?
  • 请试一试。
猜你喜欢
  • 2013-03-17
  • 2021-10-08
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多