简体   繁体   English

如何确定是否正在从 zip 文件运行 C# 可执行文件?

[英]How to determine if C# executable is being run from zip file?

I'm trying to ensure that users of my C# .NET Core program always unzip it before running, however, I can't seem to find a good solution for this.我试图确保我的 C# .NET Core 程序的用户总是在运行之前解压缩它,但是,我似乎找不到一个好的解决方案。

I tried using Assembly.GetExecutingAssembly().Location and checking if it contained Temp but it did not work as intended.我尝试使用Assembly.GetExecutingAssembly().Location并检查它是否包含Temp但它没有按预期工作。 My understanding is that when running an executable within a zipped file, Windows will automatically unzip (?) to a temp directory and then proceed.我的理解是,在压缩文件中运行可执行文件时,Windows 将自动解压缩 (?) 到临时目录,然后继续。

Any ideas on how to go about doing this?关于如何去做这件事的任何想法?

Thanks!谢谢!

One solution might be that you put another file beside the exe file inside the zip file and make the exe find for this file in the same location of the exe.一种解决方案可能是您将另一个文件放在 zip 文件中的 exe 文件旁边,并在 exe 的同一位置找到该文件的 exe。

When an exe is executed from inside a zip, the unzip program will unzip the exe but not the other file and checking for this file you can test whether just the exe was unzziped or if the whole files where unzipped and the guess it was installed.当从 zip 中执行 exe 时,解压缩程序将解压缩 exe 而不是其他文件,并检查此文件,您可以测试是否只是 exe 被解压缩,或者整个文件是否已解压缩并猜测它是否已安装。

(Anyway, I agree with Ian Kemp, as it seems to be an XY problem).. (无论如何,我同意 Ian Kemp 的观点,因为这似乎是一个 XY 问题)。

I can think of two checks that will work in many cases.我可以想到两种在许多情况下都有效的检查。 Obviously, these checks are not bullet-proof.显然,这些检查不是防弹的。

  1. Most .zip viewers will extract the clicked .exe in a temporary folder.大多数.zip查看器会将单击的.exe压缩到一个临时文件夹中。
  2. Windows Explorer zip folder view runs the .exe from a temporary folder and sets the ReadOnly attribute. Windows 资源管理器 zip 文件夹视图从临时文件夹运行.exe设置ReadOnly属性。

Sample app示例应用程序

using System;
using System.IO;

namespace SimpleApp
{
    class Program
    {
        private static bool IsLikelyRunFromZipFolder()
        {
            var path = System.Reflection.Assembly.GetEntryAssembly().Location;
            var fileInfo = new FileInfo(path);
            return fileInfo.Attributes.HasFlag(FileAttributes.ReadOnly);
        }

        private static bool IsRunFromTempFolder()
        {
            var path = System.Reflection.Assembly.GetEntryAssembly().Location;
            var temp = Path.GetTempPath();
            return path.IndexOf(temp, StringComparison.OrdinalIgnoreCase) == 0;
        }

        static void Main(string[] args)
        {
            var isTemp = IsRunFromTempFolder();
            var isZip = IsLikelyRunFromZipFolder();
            
            Console.WriteLine($"Run from TEMP folder? {isTemp}");
            Console.WriteLine($"LIKELY run from Windows Explorer ZIP folder? {isZip}");
            
            Console.ReadKey();
        }
    }
}

I disagree with the sceptics in this thread.我不同意这个线程中的怀疑论者。 Running an exe from a "zip folder" in Windows Explorer is indeed a common source of error.从 Windows 资源管理器中的“zip 文件夹”运行exe确实是一个常见的错误来源。 By showing a warning, inexperienced users could get some immediate advice.通过显示警告,没有经验的用户可以立即获得一些建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM