0

I have a problem to create exe file from assembly. In my project i have many file (jpg, txt) that i have added. When i build on my pc works fine, but if i use it in onother pc doesn't work(the path is always the same).

So i have setted recources as content and copy if newly (i have visual studio in italian language) and in the new release i have all resources, but neither works on other pc.

The path of resource is on my pc and i think that this is the problem

How i can build it?I would not provide the code. Thanks

I access to recources by directory scan:

String path = "../../../Reference";
directoryTxt = new DirectoryInfo(path + "\\txt").GetFiles()
                .OrderBy(f => f.CreationTime)
                .Select(f => f.FullName)
                .ToArray();
        directoryJpg = new DirectoryInfo(path).GetFiles()
                .OrderBy(f => f.CreationTime)
                .Select(f => f.FullName)
                .ToArray();
        directoryExercises = new DirectoryInfo("../../../ExercisesImages").GetFiles()
                .Select(f => f.FullName)
                .ToArray();
System.IO.StreamReader file = new System.IO.StreamReader("../../../MaskAngle/testExercise.txt");
luca
  • 3,248
  • 10
  • 66
  • 145
  • 1
    *I would not provide the code.* That's fine. I will not provide any help. Seriously though, perhaps I misunderstand what you meant by that statement. I think you do need to provide some more details. – David Heffernan Nov 24 '14 at 11:02
  • no, this is a misunderstanding :D I would not want to provide the code to other pc where i want use the exe – luca Nov 24 '14 at 11:06
  • OK, that makes a little more sense! Linking these files as resources should do the trick. No reason why that won't work. Although if the files are large then it would make more sense to keep them external to the executable. – David Heffernan Nov 24 '14 at 11:07
  • @luca You'll need to show small example of how you are currently accessing your resources programmatically. – Filburt Nov 24 '14 at 11:08
  • i have added an example above – luca Nov 24 '14 at 11:14
  • What exactly does not work? The project does not build or your resulting executable throws exception on another PC? – galenus Nov 24 '14 at 11:37
  • the second, on my pc works fine, but if i launch exe in another pc it crashes at starting – luca Nov 24 '14 at 11:39
  • @galenus From the example the OP added it's pretty obvious that he expects to access embedded resources (compile as content) but actually tries to load files from a filesystem path. – Filburt Nov 24 '14 at 11:48
  • @Filburt except his mention "when I build on my pc works fine" which makes the whole question not so clear. – galenus Nov 24 '14 at 11:51
  • You have to create installation file to distribute your application. This file job is to create directory structure (similar to one on your PC). This also means you have to take care to *organize* files properly. Other possibility is to embed images into resources indeed, but that would require you to change parts of code which are using them as well. – Sinatr Nov 24 '14 at 12:37
  • how i create an installation file?in visual studio or other tools(NSIS ...) – luca Nov 24 '14 at 13:10

1 Answers1

1

It is not enough to Copy to Output Directory your resources - this will result in copying the files into the build output folder on your development machine.

The simplest approach in you case would be to embed the files into the executable itself using Embedded Resource build action for each of them:

Embedded resource build action

Then, you would access the files like described here.

Note As David pointed out in the comments, this way to deploy resources is sub-optimal. For large sets of files you should consider either bringing them using some installation package or, in case you don't need all of them on startup and you have a reliable connection from the target machine to some central server, providing them on demand remotely.

Community
  • 1
  • 1
galenus
  • 2,087
  • 16
  • 24
  • This is fine until the size of the embedded files becomes large – David Heffernan Nov 24 '14 at 11:55
  • @DavidHeffernan As I said, it's the simplest approach. Otherwise he should create an installation package which will take care of deployment the resources together with the application. But that wasn't the question, as I understand. – galenus Nov 24 '14 at 11:57
  • 1
    It's the simplest approach indeed. I'm just pointing out the problem with it. In extremis, for a 32 bit process, it is possible to exhaust address space because the embedded resources are all mapped into virtual memory. It is worth making this point. So I did. – David Heffernan Nov 24 '14 at 11:59
  • @DavidHeffernan ...and I completely agree with the point made. – galenus Nov 24 '14 at 12:03
  • thanks, in the future I will have to add files gradually (jpg and txt), so what do you suggest? – luca Nov 24 '14 at 12:54
  • Invest in deployment. Investigate available setup solutions (Wix, etc.) and prepare an installer for your application. – galenus Nov 24 '14 at 13:05