1

I have an unmanaged dll so i wrote this to save the dll to file once the program is run.

Working code :

public static void ExtractResourceToFile()
{
    if (!File.Exists("loader.dll"))
        try
        {

            using (System.IO.FileStream fs = new System.IO.FileStream("loader.dll", System.IO.FileMode.Create))
                System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Kraken.Resources.loader.dll").CopyTo(fs);
                Thread.Sleep(5000);

        }       
        catch ( Exception ex)
        { }
}

Problem:

if the compiled Kraken.exe name changed the DLL is not being saved.

what I've tried :

public static void ExtractResourceToFile()
{
    if (!File.Exists("loader.dll"))
        try
        {
            string file = System.Reflection.Assembly.GetExecutingAssembly().Location;;
            string app = System.IO.Path.GetFileNameWithoutExtension(file);
            using (System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Create))
                System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(app + ".Resources.loader.dll").CopyTo(fs);
                Thread.Sleep(5000);

        }       
        catch ( Exception ex)
        { }
}

I've also tried to get the current process name and use it, but same problem occurred.

meatball
  • 11
  • 1
  • 3
    So you ignore the exceptions thrown then wonder why it does not work? – zerkms Nov 30 '16 at 02:44
  • 1
    Set the final file name when you compile so you don't have to change it afterward. If you change it afterward, I think that some information in file properties still have the original name so it would look less professional. – Phil1970 Nov 30 '16 at 02:44
  • So you expect `GetManifestResourceStream("foo")` and `GetManifestResourceStream("bar")` to consistently return stream you *want* irrespective of what arguments you pass to the method? Note that there is no direct relationship between name of assembly and default namespace.... – Alexei Levenkov Nov 30 '16 at 03:46
  • @AlexeiLevenkov i didnt understand you well, but thats what i mean, GetManifestResourceStream() requires the namespace not the assembly name – meatball Nov 30 '16 at 04:16
  • Do `Namespace.Properties.Resources.Name` useful somehow ? – meatball Nov 30 '16 at 04:20

1 Answers1

0

See this: How do I get the name of the current executable in C#?

Particularly these comments are interesting:

Beware of GetExecutingAssembly(): if you call this from a library assembly, it returns the name of the library assembly, which is different from the name of the entry assembly (i.e. the original executable). If you use GetEntryAssembly(), it returns the name of the actual executable, but it throws an exception if the process is running under WCF (admittedly a rare situation). For the most robust code, use Process.GetCurrentProcess().ProcessName. – Contango Nov 3 '10 at 12:01

Hmm, note that the returned string won't change even if you rename the executable file by hand using the file explorer. While Environment.GetCommandLineArgs()[0] changes along with the actual executable filename (of course). Coincidentally, the second method resulted better for my specific situation as I want the data folder to be named as the actual executable filename. – Hatoru Hansou Jan 23 '14 at 2:25

Hatoru's comment seems to support Phil1970's comment, that GetExecutingAssembly() probably use the resource properties found in the assembly instead of the actual file name.

So I'd use

Process.GetCurrentProcess().ProcessName

as Contango suggested.

And as zerkms said, don't ignore exceptions.

Community
  • 1
  • 1
KC Wong
  • 2,410
  • 1
  • 18
  • 26
  • If you feel that linked answer solves the issue just flag as duplicate... But in reality name of executable has nothing to do with parameter of `GetManifestResourceStream`... – Alexei Levenkov Nov 30 '16 at 03:48
  • The comments I found useful aren't in the marked answer of the linked thread. Do I flag as duplicate and leave a comment instead? – KC Wong Nov 30 '16 at 04:00
  • @KCWong i did try with ProcessName, but same problem, the exception im getting is: `System.NullReferenceException: Object reference not set to an instance of an object.` – meatball Nov 30 '16 at 04:18
  • @meatball Try to break up the line "System.Reflection.Assembly.GetExecutingAssembly()"... you want to find out if the NullReferenceException came from GetExecutingAssembly() or GetManifestResourceStream(). You should also try using a debugger, set a breakpoint on the line and step into it. – KC Wong Nov 30 '16 at 04:35
  • @KCWong i ended up using base64 and GZip to load it from the code – meatball Nov 30 '16 at 06:42