Question

From what I understand based on this and this, my app is running from a location that can be referred to with @"\Program Files\HHS\HHS.exe":

enter image description here

...and the file I want to access could be accessed via @"\Application\sscs\HHSetup.exe":

enter image description here

...but this code:

string clientVer = HHSUtils.GetFileVersion(@"\Application\sscs\HHSetup.exe"); 
. . .
public static string GetFileVersion(string filePath)
{
    try
    {
        const int VERSION_DEPTH = 4;
        var version = NativeFile.GetFileInfo(filePath);
        return version.Version.ToString(VERSION_DEPTH);
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

...fails. Instead of a version number such as "3.1.4.7" (or something sensible like "0.0.0.0" as a "can't find it" value), GetFileVersion() returns "Win32Exception."

Even when I deal with that by forcing such an "oh, well" value:

if (clientVer.Contains("Exception")) 
{
    clientVer = "0.0.0.0";
}

...the subsequent call fails with "Unable to connect to the remote server"

// both serial num and clientVer have valid values now, such as "8675309" for serialNum and "0.0.0.0" for clientVer
string uri = string.Format("http://localhost:28642/api/FileTransfer/GetUpdatedHHSetup?serialNum={0}&clientVersion={1}", serNum, clientVer);
RESTfulMethods.DownloadNewerVersionOfHHSetup(uri);

...but that's another story; my question here is: Why does GetFileVersion() return "Win32Exception"? Is the path I am sending (@"\Application\sscs\HHSetup.exe") wrong, or is it something in GetFileVersion() itself?

UPDATE

Since the code is almost guaranteed good, I'm thinking the problem must be in the path I'm passing in - it's format or such. Maybe it is a permissions error, as Michael Kniffen supposes below; but if so, I don't know how to solve it. Or maybe it's a path formatting issue? I don't know; I'm more stumped than a 19th century Oregon homestead...

UPDATE 2

It seems to be a path issue; czech this out:

//string clientVer = HHSUtils.GetFileVersion(@"\Application\sscs\HHSetup.exe");
// to see if the path is the problem, try something more straightforward:
string clientVer = HHSUtils.GetFileVersion(@"\Program Files\HHS\vsd_setup.dll");

The commented out code was causing the problem.

The new code runs okay; I get a valid file version ("1.0.2268.0").

So it almost has to be either the path ("\Application" is not correct for accessing the "Internal Flash Drive" (or whatever the correct terminology is)) OR maybe the file itself - it doesn't have file version information, so that being the case might be the cause of the problem.

I'll test this by copying vsd_setup.dll beneath the sscs folder and trying this:

UPDATE 3

It was the file - the test works fine. So if a file doesn't have version information, a Win32Exception 13 is returned.

If the file being sought has version info, I get it (such as "1.0.2268.0"); if it doesn't, though, watch out below - all Dallas breaks loose!

string clientVer = HHSUtils.GetFileVersion(@"\Application\sscs\vsd_setup.dll");
Was it helpful?

Solution

Why not start out with a simple app that you just run on the device that runs through the basics? This will tell you if it's a path problem or not. Not having a live debugger doesn't mean you should forego the debugging fundamental of "start simple".

public static void Main()
{
    var path = "\\my\\file\\path.foo";

    Console.WriteLine("Checking for file...");
    var fi = new FileInfo(path);
    if(!fi.Exists)
    {
        Console.WriteLine("File doesn't exist");
        return;
    }

    try
    {
        Console.WriteLine("Getting version...");
        const int VERSION_DEPTH = 4;
        var version = NativeFile.GetFileInfo(path);
        Console.WriteLine(version.Version.ToString());
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: " + return ex.Message);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top