Managing folders in a deployed application

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
20,106
I have this desktop application I've doctored up for a friend. One of its functions is to generate a PDF document from an HTML Template.

I have a folder in the project directory which holds the template. This is how I navigate to that folder in the application: string templateFolder = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString() + @"\templates\";

I compile the app and take all of the files from the bin/debug and place them in a separate folder along with the templates folder.

When it is on his machine and I run the app, it throws an error saying cannot the template file in 'file:///C:/Users/Dev03/templates/HTMLTemplateForJson.html'. It is completely bypassing the app altogether.

I've not had to deal with paths and directories in this way before. How do I get around this please guys?
 

mister

Executive Member
Joined
Jul 21, 2008
Messages
8,752
var assemblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);

var templateFolder = Path.Combine(assemblyPath, "templates");
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
20,106
var assemblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);

var templateFolder = Path.Combine(assemblyPath, "templates");

It works and doesn't work. This seems to be the issue here, the boldened bit.

"file:\\C:\\Users\\Dev03\\Documents\\InvoiceMaker\\InvoiceMaker\\bin\\Debug\\templates\\"

Edit: Yeah this seems to do the job:

C#:
AppDomain.CurrentDomain.BaseDirectory + @"templates\";
 
Last edited:

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
20,106
I think his machine is configured strange. Looks like Dropbox has messed with his pathing on a lot of directories. I've just put the project on C:\ and that takes care of that.
 

Spacerat

Expert Member
Joined
Jul 29, 2015
Messages
1,001
Mister’s suggestion is correct. I think there is something else wrong in yr app. However I would use GetEntryAssembly as opposed to GetExecutingAssembly. The former returns the exe location as opposed to the latter returning the path to the assembly executing the current code. Subtle diff.
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
20,106
Mister’s suggestion is correct. I think there is something else wrong in yr app. However I would use GetEntryAssembly as opposed to GetExecutingAssembly. The former returns the exe location as opposed to the latter returning the path to the assembly executing the current code. Subtle diff.

Thanks Spacerate. I have reverted back to Mister's suggestion, it is better yes. There was something wrong with the machine the project was being deployed on. I check in the registry and even the pathings are all screwed up. Looks like Dropbox did a number on where all the folders are supposed to be.

Anyway @mister thanks again, your code was much better and works fine now!
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
20,106
Found another one which may be helpful.

C#:
        public static string AssemblyDirectory
        {
            get
            {
                string codeBase = Assembly.GetExecutingAssembly().CodeBase;
                UriBuilder uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                return Path.GetDirectoryName(path);
            }
        }


This is helpful if you keep getting this boldened bit "file:\\C:\\Users" when trying to get the assembly path. On some machines there is an issue I've not fully gotten to the bottom of yet. So when using the following:

C#:
var assemblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);

var templateFolder = Path.Combine(assemblyPath, "templates");

or even this:

C#:
AppDomain.CurrentDomain.BaseDirectory

you end up with something I've described above. The answer from stackoverflow seems to forcefully strip that file path away..
 
Top