Directory problems

Asked

Viewed 4,129 times

3

I am trying to read a file . html but I cannot get the path of this file. I have already used Environment.GetCurrentDirectory and Directory.GetCurrentDirectory but did not succeed. Below follows code I am currently trying, but it tries to recover the solution execution path.

var foo = File.ReadAllText(@"~/EmailTemplate/email.html");

With this code, I get the following error:

Unable to locate part of the path 'C:...... bin Debug ~ Emailtemplate email.html'.

  • What kind of application? Web, WCF, Webservice, Winforms???

3 answers

3


It depends on where you want to get the current directory you should do it:

using System.Console;
using System.IO;
using System.Reflection;

public class Program {
    public static void Main() {
        WriteLine(Path.Combine(Directory.GetCurrentDirectory(), @"EmailTemplate/email.html"));
        WriteLine(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"EmailTemplate/email.html"));
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

The first form with Directory.GetCurrentDirectory() takes the current directory that the application is accessing. It can be the same directory as the application or not. You have to see if this is what you want.

If you want to make sure you take the location where the app is you have to use the second form that picks up the Assembly.GetExecutingAssembly().Location.

If the criterion is another to find out which directory should be used as the basis, you have to find out what the criterion is and choose the method that returns the appropriate information. If neither of these solve the problem because the basis is different, give more information on the question for me to put a new option.

Note that the Path.Combine() is used to turn the two parts of the path into a valid and complete path.

As a curiosity Environment.CurrentDirectory() produces exactly the same Directory.GetCurrentDirectory(). In fact one calls the other.

  • The way you put it, if its application is an executable, the Assembly.GetExecutingAssembly().Location will not return the path of .exe? Ai in the Path.Combine no error will occur?

  • @Richarddias as far as I know no, you have some information that shows it? I can’t guarantee. The application is an executable and seems to be working as demonstrated. I’m not saying that there isn’t some situation that this fails, if you know it and can demonstrate it, it would be interesting for us all to learn. As far as I know he only takes the part of the directory to make the combination, so if he has a file that he’s referencing doesn’t matter, he knows to clean up and take only what matters.

  • I said this because according to the example I posted, using an executable, it returned the file path .exe, so I used a FileInfo to get the name of the directory where the .exe is being executed. I was doing this only yesterday, and this was the solution I found to resolve a similar situation.

  • @Richarddias I think I now understand what you’re saying, I’ve made a change that I think solves the question.

1

Kleyton, this system variable will take the directory where the application is running, so it will display a slightly different behavior in the release version than in debug.

In your case, I see that it is interesting you get the Project/Publication address.

You can access it using: AppDomain.CurrentDomain.BaseDirectory

1

Try to use the following:

var fi = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
//No exemplo citado presumo que seja um executável
var pastaExe = fi.Directory.FullName;

var caminhoArquivo = System.IO.Path.Combine(pastaExe, "EmailTemplate/email.html");

I believe this will meet your need and you should no longer worry about the file path.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.