Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Retrieving file version causes the file to remain open

Posted on 2007-10-08
10
Medium Priority
?
578 Views
Last Modified: 2009-06-13
               string filename = @"\\UNC file name";
                Assembly asm = Assembly.LoadFile(filename);
                int tMajor = asm.GetName().Version.Major;
                int tMinor = asm.GetName().Version.Minor;
                int tBuild = asm.GetName().Version.Build;
                int tRevision = asm.GetName().Version.Revision;

the above method works fine for retrieving the build information; however, it keeps the file open.  

How can i close the file ( there is no dispose or close function on asm ) after I retrieve the version information?  Is there a better way or retrieving the version info?

Thanks for the help.
0
Comment
Question by:freezingHot
  • 6
  • 3
10 Comments
 
LVL 11

Assisted Solution

by:udhayakumard
udhayakumard earned 600 total points
ID: 20034132
Assembly myAssmebly = Assembly.ReflectionOnlyLoadFrom(@"\\UNC file name");

string qualifiedName = myAssmebly.FullName;

 

this approach does not load the assembly in the domain.
0
 
LVL 1

Author Comment

by:freezingHot
ID: 20034320
as soon as .ReflectionOnlyLoadFrom() is executed, the file name is listed as Open in the file share list on the server.  when the application closes, the file is no longer listed as open.

unfortunately, the ReclectionOlyLoadFrom() is acting the same way.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 900 total points
ID: 20040352
You might get away with AppDomainSetup.ShadowCopyFiles to prevent the files from getting locked:

using System;
using System.Reflection;

public class AssemblyInfo
{

    public static string GetAssemblyVersion(string fileName)
    {
        AppDomainSetup setup = new AppDomainSetup();
        setup.ShadowCopyFiles = "true";
        AppDomain domain = AppDomain.CreateDomain("AssemblyLoader", AppDomain.CurrentDomain.Evidence, setup);
        Assembly assembly = domain.Load(fileName);
        return assembly.GetName().Version.ToString();
    }

}

Bob
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:freezingHot
ID: 20040570
having a heck of a time passing it the filename -

my filename is something like this:

string fileName = @"\\192.168.100.1\publicDrive\app.2007\application.2007.exe";

the domain.Load(fileName); is failing.  It says that it can not find the specified file.  the file does exist.  I tried to change the ApplicationBase in the AppDomainSetup variable, but that didn't help.

is there a better way to obtain version information from a file - it seems like a trivial task.

I appreciate your help.


Thanks.

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20040601
192.168.100.1 is a private network address, so does it fail if you don't use a UNC path with an IP address, and use a mapped network drive instead?

Bob
0
 
LVL 1

Author Comment

by:freezingHot
ID: 20040669
i receive, "could not load file or assembly or one of its dependencies.  The given assembly name or codebase was invalid. (HRESULT: 0X80131047)"
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20040804
What code were you running when you got the exception (initial code or revised code from comment above)?

Bob
0
 
LVL 1

Author Comment

by:freezingHot
ID: 20040848
that's from the mapped network drive part.
0
 
LVL 1

Author Comment

by:freezingHot
ID: 20040884
i found this which works much better.  it places the initial lock on the file; however, it drops the open lock after the function has completed:

System.Diagnostics.FileVersionInfo fileVersInfo =  System.Diagnostics.FileVersionInfo.GetVersionInfo(filename);
int tMajor = fileVersInfo.FileMajorPart;
int tMinor = fileVersInfo.FileMinorPart;
int tBuild = fileVersInfo.FileBuildPart;
int tRevision = fileVersInfo.FilePrivatePart;
0
 
LVL 1

Author Comment

by:freezingHot
ID: 20040895
i appreciate the help from both of you - thanks again.  I awarded points to both for taking the time to help me out.

have a good one.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month10 days, left to enroll

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question