Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取exe文件的版本而不在.NET Compact Framework中加载_C#_.net_Compact Framework_Windows Ce_.net Cf 3.5 - Fatal编程技术网

C# 获取exe文件的版本而不在.NET Compact Framework中加载

C# 获取exe文件的版本而不在.NET Compact Framework中加载,c#,.net,compact-framework,windows-ce,.net-cf-3.5,C#,.net,Compact Framework,Windows Ce,.net Cf 3.5,我有一个运行在.NET Compact Framework 3.5上的移动应用程序和一个运行在同一平台上的更新程序应用程序。当用户点击应用程序快捷方式时,更新程序应用程序首先运行,并检查主应用程序是否有新版本。有空。为此,我使用assembly.LoadFrom方法加载主exe程序集并获取当前版本。如果找到新版本(通过web服务),它将下载新文件并替换。这个很好用。问题是,当它尝试替换主exe文件时,它会失败,出现“由另一个进程使用”样式的异常(可能是因为它以前已经加载过)。如何卸载此程序集,或

我有一个运行在.NET Compact Framework 3.5上的移动应用程序和一个运行在同一平台上的更新程序应用程序。当用户点击应用程序快捷方式时,更新程序应用程序首先运行,并检查主应用程序是否有新版本。有空。为此,我使用assembly.LoadFrom方法加载主exe程序集并获取当前版本。如果找到新版本(通过web服务),它将下载新文件并替换。这个很好用。问题是,当它尝试替换主exe文件时,它会失败,出现“由另一个进程使用”样式的异常(可能是因为它以前已经加载过)。如何卸载此程序集,或者如何在不加载的情况下获取其版本

我对汇编类和AppDomain做了一些研究,但是.NETCF有一些局限性,所以我无法理解

有什么想法吗

谢谢。

使用
AssemblyName.GetAssemblyName(string)
FileVersionInfo
的“传统”方式将不起作用,因为它们在.NET CF上不受支持。要在不使用
Assembly.LoadFrom
的情况下执行此操作,您需要使用p/Invoke以本机方式获取文件版本信息。您可以尝试此代码(未经测试):


旧帖子,但万一有人找它。 我的VB(工作)版本


我尝试了这个方法,但GetFileVersionInfo方法返回了零。我想我将遵循一种更简单的方法,比如将当前版本信息存储在文本文件中,并在存在新版本时更新它。谢谢您的帮助。@DenizAlpaslan您是否已验证该文件确实具有文件版本?您还可以调用
Marshal.GetLastWin32Error()
以获取返回的错误代码,并查看其失败的原因。Windows Mobile上有许多文件(DLL、exe)没有文件版本信息资源。CF应用程序没有这些默认的res,因此最好是让CF exe将其“版本信息”写入txt或注册表以进行外部检查。
[DllImport("coredll", EntryPoint = "GetFileVersionInfo", SetLastError = true)]
private static extern bool GetFileVersionInfo(string filename, int handle, int len, IntPtr buffer);
[DllImport("coredll", EntryPoint = "GetFileVersionInfoSize", SetLastError = true)]
private static extern int GetFileVersionInfoSize(string filename, ref int handle);
[DllImport("coredll", EntryPoint = "VerQueryValue", SetLastError = true)]
private static extern bool VerQueryValue(IntPtr buffer, string subblock, ref IntPtr blockbuffer, ref int len);

public static Version GetFileVersionCe(string fileName)
{
    int handle = 0;
    int length = GetFileVersionInfoSize(fileName, ref handle);
    Version v = null;
    if (length > 0)
    {
        IntPtr buffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(length);
        if (GetFileVersionInfo(fileName, handle, length, buffer))
        {
            IntPtr fixedbuffer = IntPtr.Zero;
            int fixedlen = 0;
            if (VerQueryValue(buffer, "\\", ref fixedbuffer, ref fixedlen))
            {
                byte[] fixedversioninfo = new byte[fixedlen];
                System.Runtime.InteropServices.Marshal.Copy(fixedbuffer, fixedversioninfo, 0, fixedlen);
                v = new Version(
                    BitConverter.ToInt16(fixedversioninfo, 10), 
                    BitConverter.ToInt16(fixedversioninfo,  8), 
                    BitConverter.ToInt16(fixedversioninfo, 14),
                    BitConverter.ToInt16(fixedversioninfo, 12));
            }
        }
        Marshal.FreeHGlobal(buffer);
    }
    return v;
}
Public Declare Function GetFileVersionInfo Lib "Coredll" (ByVal filename As String, ByVal handle As Integer, ByVal len As Integer, ByVal buffer As IntPtr) As Boolean
        Public Declare Function GetFileVersionInfoSize Lib "Coredll" (ByVal filename As String, ByRef handle As Integer) As Integer
        Public Declare Function VerQueryValue Lib "Coredll" (ByVal buffer As IntPtr, ByVal subblock As String, ByRef blockbuffer As IntPtr, ByRef len As Integer) As Boolean

Public Function GetFileVersionCE(ByVal fileName As String) As Version
            Dim handle = 0
            Dim length = GetFileVersionInfoSize(fileName, handle)
            Dim v As Version = Nothing

            If length > 0 Then
                Dim buffer As IntPtr = Marshal.AllocHGlobal(length)
                If (GetFileVersionInfo(fileName, handle, length, buffer)) Then
                    Dim fixedbuffer As IntPtr = IntPtr.Zero
                    Dim fixedlen As Integer = 0

                    If (VerQueryValue(buffer, "\\", fixedbuffer, fixedlen)) Then

                        Dim fixedversioninfo(fixedlen) As Byte
                        Marshal.Copy(fixedbuffer, fixedversioninfo, 0, fixedlen)
                        v = New Version(BitConverter.ToInt16(fixedversioninfo, 10), _
                                        BitConverter.ToInt16(fixedversioninfo, 8), _
                                        BitConverter.ToInt16(fixedversioninfo, 14), _
                                        BitConverter.ToInt16(fixedversioninfo, 12))
                    End If
                End If
                Marshal.FreeHGlobal(buffer)
            End If

            Return v

        End Function