System.Reflection.Assembly.GetExecutingAssembly. What am i actually looking at? - c#

I have been trying to get any one of the following directories:
Root of solution folder
Startup project folder
I came across this:
System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location );
but i'm unsure what it's actually showing me.
for example,
the folder i'm trying to get to is:
C:\Users\James\Documents\Visual Studio 2013\Projects\Test\Test.WebUI\Uploads
And the above function returns this:
C:\Users\James\AppData\Local\assembly\dl3\1RC2V770.35T\WML2RQJT.GX5\9ebc1d95\b8f9a830_5676d001
Is this at all helpful to me?
The context of the issue is that i'm running an entity framework seeder and need access to the startup projects folder structure.

What you are seeing is the shadow copy folder that .NET uses for cache. It is specified in Windows Registry under HKCU\Software\Microsoft\Fusion\DownloadCacheLocation. Shadow copying is a feature in the .NET framework to allow assemblies used in an app domain to be updated without unloading the app domain.
There is a similar question here.
That is the reason why you can't retrieve your original folder location using System.Reflection.Assembly.GetExecutingAssembly().Location
You might want to look at AppDomainSetup.ShadowCopyDirectories if you want to change/read the directories where the dlls are stored.
Hope it helps

Related

OBS libobs-sharp - Cannot get to run

I would like to interact with OBS via a c# wrapper.
I have come across the following, which looks like it could help, but I am struggling to get anything to work.
https://github.com/ilosvideos/libobs-sharp#libobs-sharp
It has the following installation instructions, however I am unsure exactly what these mean? I'd like to be able to include the libobs-sharp dll in my project and access the functions it gives, I have tried adding the dll to the C:\Program Files (x86)\obs-studio\bin\64bit folder but this hasn't worked.
Has anyone any suggestions?
Installation
Recommended: Place libobs-sharp to root of the obs-studio directory (obs-studio/libobs-sharp)
Debugging: Use the obs-studio output folder as project working directory (obs-studio\build\rundir\Debug\bin\32bit)
Binaries: In the same directory as other obs-studio output files.

How is it possible to access files in project directory using relative path, regardless of executable location?

While running my project using Visual Studio, the working directory (AppDomain.CurrentDomain.BaseDirectory) is {ProjectDirectoryPath}\bin\Debug\netcoreapp2.0; but while running the project from its publish folder (after publishing it), the working directory is {PublishDirectoryPath}. All project configuration and data files are located under {ProjectDirectoryPath} and {PublishDirectoryPath} in the same tree structure. What it means is that whenever I'm running the project using Visual Studio, I should prefix the path of each of the files with ../../../.
My current solution is using the following function whenever accessing a file in the project directory:
public static string Root()
{
string root = AppDomain.CurrentDomain.BaseDirectory;
if (root.EndsWith("Debug\\netcoreapp2.0\\"))
root = root + Slash("../../..");
return root;
}
However, it feels to me that there must be some better way to solve this, since using files located under the project directory is nothing unusual (though it's more common to store configuration and data files under %AppData% or /home). So it doesn't seem right that so many developers would really implement a solution like this to something so common.
What am I missing?
Typically, any constants that your application is dependent on that could change are added to a web.config/app.config file; so that you don't need to recompile the code to change these values. An exception would be a service or library that would require consumers to provide the data via parameters.
When you store paths in a config file, you can use web.config/app.config transforms to change the values based on the build configurations. This allows you to supply different values to your config file settings based on the environment you deploy to.
I posted this question after hours of research. Even after posting it I continued searching for a solution. I saw a lot of threads about this issue, and a lot of solutions that didn't work in my case. Now I stumbled across this, and I used the second approach. Using this solution it works.
It seems so bizarre to me that the access to something so basic would be so non-intuitive. Unfortunately, .NET Core is yet not mature enough.

Programmatically get Folder location for assembly

Shadow copying is a feature in the .NET framework to allow assemblies used in an app domain to be updated without unloading the app domain. Very useful in web site binary updates update/release for example.
Is there a programmatic way to read the formal location assembly folder i.e. the website \bin folder instead of the shadow copy folder?
For example when GetExecutingAssembly like this:
System.Reflection.Assembly.GetExecutingAssembly().Location
It returns ShadowCopy location folder:
C:\Windows\system32\config\systemprofile\AppData\Local\assembly\dl3\4LPBW244.CR4\DZO34RQG.YAW\caa6207b
I want to obtain the the \bin folder as per my example...
4 years without a response!
And I needed a response to this exact question!
I finally found a possible answer. This is what I did when trying to obtain the Location of an assembly. The assembly file name was "IKVM.Runtime.DLL"
System.Reflection.Assembly.ReflectionOnlyLoad("IKVM.Runtime").Location
Note the the file extension had to be removed!

Visual Studio Moving a Content File on Publish

I'm developing a Web application that uses a couple XML files to store data. I have their Build Action set to Content, and on install the files are copied successfully to the Applications Virtual Directory:
C:\inetpub\wwwroot\ApplicationName\
The problem I'm having is that writing to these XML files (in order to save settings and things like that) causes a lot of write permissions issues. Therefore to get around it, I'm trying to copy these files from the virtual directory they're installed to to a new directory under the C drive, using the following PostBuildEvent in the Web Deployment Project:
xcopy "$(TargetDir)*.xml" "C:\CompanyName\ApplicationName\" /y
However, this does nothing. I'm not sure if this is because PostBuildEvents in the installer are not actually fired on install, but only on building the installer, or if TargetDir represents the bin directory:
C:\inetpub\wwwroot\ApplicationName\bin\
instead of the root application virtual directory:
C:\inetpub\wwwroot\ApplicationName\
Does anyone have any ideas? Has anyone dealt with this sort of thing before? I'm really stumped on this one.
Update:
I included a PostBuildEvent that should give full permissions to all users:
icacls "$(TargetDir)" /grant Users:F
But it doesn't seem to have resolved the problem.
I'm also unsure where exactly $(TargetDir) if pointing to, if it would be ..\ApplicationName\ or ..\ApplicationName\bin\
All you have to do is give full permissions to the user under whose context the Web Application Pool runs.
You can find this user by starting IIS manager and look at the Application Pools and Identity column
Something like
cacls C:\inetpub\wwwroot\ApplicationName /G Users:F
will give all permissions to this subdirectory to all users on the computer, If you are running under ApplicationPoolIndentity refer here
Turns out there's a much easier way to do what I was trying to do.
Instead of using PostBuildEvents to create a new directory structure and move files there, I added the new directory structure into
InstallerProject > View > File System
after which I located the Content Files from SomeProject entry in my installer project, and changed the Folder value to the newly specified directory structure, in this case
C:\CompanyName\ApplicationName\
This seems to work fine, I'm now able to access these files freely as I originally intended.

Access project files from class in separate folder

Screenshots below are from the Solution Explorer Pane:
I have a class in Folder DataAccess that needs to access a resource in Folder data. I get the following error message.
System.IO.IOException: Cannot locate resource 'data/danio_rerio.xml'.
Does not Work If:
It works if:
Folder data is a child folder within Folder DataAccess. Is it possible for me to use the following syntax:
foldername/somefile.someextension
to access resources within the project that are in seperate folders?
Edit: I do not want to hard code locations to my directories.
When you run your application, the running directory becomes your "active directory"
Suppose you have
c:\myproject\bin\debug\myapp.exe
When you run the application you try to search
c:\myproject\bin\debug\foldername\somefile.someextension
what you actually want could be close to
c:\myproject\data\daniorenio.xml
so you'll want to search the following directory
c:\myproject\bin\debug\..\..\data\daniorenio.xml
The .. operator tells that you want to go back in the directory hierarchy
However this is assuming your binary will be in a lower branch than your ressource. What I actually do is copy everything I absolutely need for runtime inside a special directory such as
c:\myproject\RuntimeRequired
This way I can issue a post build event like this (In Project/Properties/Build-Events/Post-Build)
copy /Y "$(ProjectDir)RuntimeRequired*" "$(OutDir)"
I found the answer myself. Should have searched more before offering a bounty -_-
Pack URIs in WPF [Source]

Categories