how to get the installation path in C#? - c#

I am modifying a .NET v4.5.2 code, and I want to be able to get a script path from the installation folder (so when a user install the app, the app is able to access and run the script)
I tried Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) method but it returned the wrong value (it returned the path: "C:\Users\HP\AppData\Roaming" , whereas my program location (after installation) is "C:\Program Files (x86)". I also tried Environment.GetCommandLineArgs()[0] and Path.GetDirectoryName(appFileName) but they returned the location of the file where the code was written. could you please help me?

try AppDomain.BaseDirectory()
String _path = System.AppDomain.CurrentDomain.BaseDirectory;

Related

Reference (text) files in compiled MacOS app (application file)

I'm working with Avalonia to create a MacOS desktop program using c#. I can run the program locally or output it to a final app file using dotnet.bundle. Attached image shows the contents of the app file using Mac's "show package contents".
There are files that are deployed to the app bundle that I would like to read when running the app, but don't know how to reference them. Since this is in C#, I can't find any docs similar to what apple provides here https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html#//apple_ref/doc/uid/10000123i-CH104-SW1
I've tried referring to the parent directory, but this only works in design time.
I've tried a variety of syntax including Environment.GetCurrentDirectory as prefix to the below path.
string path = "Repository/CreateTables.sql";
string assets = File.ReadAllText(path);
Thanks
Thanks for the tip, yes I have been trying to print out paths but oftentimes they were blank when run from the .app (for example, Environment.CurrentDirectory returns ""). I did find a way to get at the path here https://github.com/AvaloniaUI/Avalonia/issues/5443#issuecomment-775906396, and it was not the path I was expecting. I thought the location of the file would start at myapp.app, but you need the path to myapp.app as well.
/Applications/MyApp.app/Contents/MacOS/Repository/CreateTables.sql

How to get the path of the uploaded file within the AppBundle?

We are using Autodesk Forge's Design Automation API. We have an AppBundle ready and we put an .rfa file into the same folder which contains the .dll file. When the AppBundle is unzipped on the Forge servers, which path can lead to our .rfa file? (how can we access it?) Our goal is to place the attached Family file's contents into the input file which is being uploaded with the API, and the result should be a new file which contains the additions from the file which we uploaded within the AppBundle. The process works when testing with Revit locally, but it doesn't work with the API. In the report we are retrieving it's obviously pointing out that the attached file cannot be found:
Autodesk.Revit.Exceptions.FileNotFoundException: T:\Aces\Jobs\ced628d35ecf4412b68c024e2cec098b\something.rfa
On the code side, we are trying to access the .rfa file via this path:
static string currentDir = Environment.CurrentDirectory;
static string path_input = currentDir+#"\something.rfa";
This seemed as a logical path, but as it turned out, it's not..
Is there a way to access the .rfa file inside the uploaded AppBundle?
I took a look at the Restrictions but reading the file from the AppBundle is not mentioned as restricted or not approachable. Am I missing something?
A .NET assembly knows its own path. You can call System.Reflection.Assembly.GetExecutingAssembly().Location within it to find the current path of the dll. You can then compute the path of the .rfa file relative to the folder of the dll and use it / open it. Thus you should be able to open any file you package along with your addin in your appbundle.
You can simply modify your code to:
static string assemblyLocation = Assembly.GetExecutingAssembly().Location;
static string assemblyDirectory = Path.GetDirectoryName(assemblyLocation);
static string path_input = assemblyDirectory+#"\something.rfa";
One thing to note however, is that you only have readonly access to files in appbundle. If your code relies on modifying these during execution, then you may simply copy the source rfa file to the current working folder and then work with the copied file instead.
Also see more details in blog for similar ideas.
We have a blog post on how you can either pass in the app bundle path in the commandLine parameter or find the path via the location of the add-in dll:
https://forge.autodesk.com/blog/store-template-documents-appbundle

windows installer cannot access directory

I am trying to run some custom actions code for windows service (the service is LocalSystem account) with windows service installer and i get the following error message:
error message while installing MSI:
Error 1001. An Exception occurred in the OnAfterInstall event handler
of System.ServiceProcess.ServiceInstaller. --> Access to the path XXX
is denied.
this code is throwing the error:
protected override void OnAfterInstall(IDictionary savedState)
{
string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
System.IO.File.WriteAllText(path, "test");
}
At the code i am trying to access the service .exe directory so i could delete the files created there
My goal is to make a custom action for the install/uninstalling process. i want to delete files that was created after installation like logs and configuration file.
Thanks
You are trying to write text to a directory instead of a file. The variable "path" is returned from Path.GetDirectoryName() which is a directory. In the next line, you are trying to do File.WriteAllText() to this variable, thus the error.
Path.Combine: As already mentioned by others, you need to specify a proper full path (path and file name). Maybe use Path.Combine? For example:
System.IO.File.WriteAllText(Path.Combine(path, "filename.txt"), "test");
Alternatives: I am not a .NET expert, and I don't use managed code custom actions. However if they are DTF based I am not sure if they have any clunk with regards to current directory or executing directory. Listing some further links:
Adding a note about: Environment.SpecialFolder (get all kinds of special folders) and Environment.CurrentDirectory (get current directory).
Reflection might be better, but check this answer: How to get execution directory of console application (all answers). There is also: AppDomain.CurrentDomain.BaseDirectory: Getting the absolute path of the executable, using C#?.
And then there is the suggestion to use GetEntryAssembly: C# – Getting the Directory of a Running Executable.
Directory.GetCurrentDirectory Method.

mysqldump.exe and mysql.exe access in C#

I'm writing an application which starts through command prompt mysql.exe and mysqldump.exe.
The idea is to make a backup and then upload it to mysql with different database name.
The problem is that I didn't think what would happen if these mysql.exe and mysqldump.exe are not registered in the cmd and you have to type the path to them manually.
And my application breaks.
So I'm wondering is there any automatic way to find the path to these two files to run them correctly.
The other solution that comes to my mind is to let the user browse his files and select these executables.
To manage this path you can use an environment variable and get it from your application.
string sPath = System.Environment.GetEnvironmentVariable("MYSQL_DIR");
Other solution is copy both executables to your application path and access them with a relative path.
string sPath = "./";
Hope it helps.

Get full path to file while debugging using IIS Express

I have a .NET application that I am trying to debug and part of my application loads a file from my project. This file is located at
C:\Users\USER_FOLDER\Documents\Visual Studio 2012\Projects\MY_PROJECT\_templates\myFile.html
In my code, I specify a relative path to the file and use the DirectoryInfo class to get the full directory path to my file:
string myFile = (new DirectoryInfo("_templates/myFile.html")).FullName;
However, this returns the following path (extra \'s as escape characters):
"C:\\Program Files\\IIS Express\\_templates\\myFile.html"
I was expecting the path that is returned when debugging in IIS Express would match the first path I listed, not the third. Why is this? Is there something else that I need to set up in my project to have it derive the paths properly? I'm assuming that this would not happen if I deployed my code to a IIS7 site, but I haven't gotten to that testing level yet.
Use Server.MapPath:
Server.MapPath("~/_templates/myFile.html")
or HttpServerUtility.MapPath:
HttpServerUtility.MapPath("~/_templates/myFile.html")

Categories

Resources