-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Environment ApplicationDirectory #41341
Comments
Maybe a short version: public string? AppDirectory { get; }
...
string configFile = Path.Combine(Environment.AppDirectory, "appSettings.json"); |
I suspect that is the plan anyway, but just to spell it out (correct me, if I'm wrong): The return value will be the full, physical path - with symlinks, if any, resolved - of the directory in which the executable is located, so that, say, an executable [Applies only to regular, application-specific executables - see next comment] |
I would assume all symlinks would be followed, yes.
No, this wouldn't be the case for all scenarios. One easy example is when you use Other scenarios where your above definition is incorrect could potentially be Mobile/Xamarin scenarios. My thought is that the |
@eerhardt, so this basically gets the path to the folder containing the file that has the managed |
Do you know how we would implement this differently from |
We have deprecated the "expand to temp single-file" publishing scheme that had this problem. The default single-file experience does not have this problem anymore.
This implementation would not work for single-file where |
My thinking was that it would be an AppContext property that was passed from the host. Similar to how Lines 46 to 47 in cc9d465
We definitely wouldn't want to use Assembly.Location, because we want this API to work in single-file and NativeAOT.
I think yes (more or less). For "mobile" scenarios or some other app scenarios, it might not be exactly that definition.
Can we update documentation then? That way in the future we can guarantee that's what |
How would the hosts that we maintain compute the value? What are the cases where this would be different from
The documentation has a text to clarify this "In .NET 5 and later versions, for bundled assemblies, the value returned is the containing directory of the host executable." What else would you like to see the documentation to say? |
I would like to see the - Gets the file path of the base directory that the assembly resolver uses to probe for assemblies.
+ Gets the file path of the base directory of the application. That way in the future when we have some new app model (shadow-copy? another extract assemblies thing?) that probes for assemblies in a different directory than where the application lives, we ensure that |
Sounds good to me. |
Just wanted to add to this conversation and provide an easy to repro example. I just wanted to share the results of some testing I did and hopefully add some to this issue... or get told what I'm doing wrong thats causing this behavior :). This is going to be pretty long, because my company won't let me post a gist... Created a new dotnet 8 console app in Visual Studio 2022 (called WhereAmI) WhereAmI.csproj
Program.cs
I built the project using Below are the results from testing
1. Windows Single File Exe
2. Windows run with dotnet
3. Linux Single File
4. Linux Run With dotnet
As you can see from the results here there is no consistent approach to getting the location of the executable. Running in different ways gives different results making it quite awkward to reliably get the location and load things like config files, images or any other external, run time loaded resource. Hope this was helpful in someway |
Background and Motivation
The path of the current application is often needed to load files in your application directory. For example,
appSettings.json
configuration files, or image files.This API is needed more than before now that we support single-file publishing and assemblies do not have physical file paths anymore. See https://github.com/dotnet/designs/blob/master/accepted/2020/form-factors.md#single-file for details.
We have
AppContext.BaseDirectory
, but that API is documented as:There are scenarios where
AppContext.BaseDirectory
doesn't return the directory where the application lives, see #40828 (comment) for example - PublishSingleFile in 3.0. SinceAppContext.BaseDirectory
is defined this way, it makes it unusable for the above purposes.Proposed API
Usage Examples
The text was updated successfully, but these errors were encountered: