So the following script is supposed to download a file to the location where the application is located. Now I Only can get GetTempPath to work. But that downloads file to the temporary folder. But I need it to download to the folder where the application is located.
var output = System.IO.Path.Combine(System.IO.TempPath(), fileName);
And when I try different things instead of GetTempPath it states method not found.
var folder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
This will get the folder of the code of the currently executing assembly, basically the assembly which is executing your code.
Related
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
I have a project into which I added a JSON file, but when I try to read the file in code I get this error.
Could not find file 'C:\\Program Files (x86)\\IIS Express\\client_secret.json'.
"ExceptionType": "System.IO.FileNotFoundException"
I have the file added in like this.
I can access it when I copy the full path and then read it.
its Asp.NEt or WEBAPI, i.e. its Web application you can access path by using Server.MapPath method.
Example : Server.MapPath("~/script/data.txt")//this locate file in your script folder on sever
for the folder under than website you need to do like this
Server.MapPath(~/client._secret.json) //here ~ sign means relative path from root
Server.MapPath method gives you physical path of your file on server machine.
I have an ASP.NET Web API project with a number of JSON files that I would like to reference in the code. I have them set to "copy-always" and they show up in the output directory on builds. The problem is I don't know how to actually reference the output directory.
I've seen a lot of answers to similar questions that say to use System.GetAssembly and use that to find the file path. However this does not work at all for web applications, because the GetAssembly or GetProcess methods just give me the IIS or IISExpress directory, not the project output folder.
How do I reference items in the project output folder without having the process running from that folder?
Edit: When I load the file by name, I get:
An exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll but was not handled in user code
Additional information: Could not find a part of the path 'C:\Program Files (x86)\IIS Express\myfile.json'.
Referencing by name looks in the IISExpress folder.
Relevant code snippet:
string file = "myjson.json";
var response = Request.CreateResponse(HttpStatusCode.OK);
System.IO.StreamReader myFile = new System.IO.StreamReader(file);
string JSON = myFile.ReadToEnd();
response.Content = new StringContent(JSON);
return response;
If you don't have any reason to put json file in your output directory then use Content folder, place your json file in content folder and refer it like this,
var File = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/myjson.json");
You can refer any resource that is in your project's output directory by its name. For example a file with the name "File1.txt" can be accessed as follows:
var fileStream = File.Open("File1.txt", FileMode.Open);
In order to get absolute path in case of IIS, you can take the following approach:
var absolutePath = HttpContext.Current.Server.MapPath("/");
I have a class in C# that saves an error message in a log file in case of an exception. Now I want to save the log file in the same folder containing the application's (in my case, a website) files. I tried using Environment.CurrentDirectory however it is not retrieving the path to my website. What can I do please to make use of a relative file path which points inside the website's directory?
Here is the class' code. As you can see, the path is absolute. I want to change it to a relative file path pointing to a folder in my website's directory.
Usually Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) returns the path where the current assembly resides. You could use
string logName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MyLogFile.log");
to create the log file name.
Question is really whether logging to the application's folder is permitted by the OS. Also, for Web-applications, the log file would be publically visible and accessible through the web browser.
For a website use:
HttpContext.Current.Server.MapPath("~/");
You might also could try this solution:
string path = AppDomain.CurrentDomain.BaseDirectory + "anotherFolder";
This would put the base dir of the app and a folder inside of the project!
I had this console application. Now i have added Environment variable PATH to its setup so that it can be executed from any location through Console. Strangely, the same application is breaking after this change.
Installation directory contains, BIN and CONFIG folder. Exe is placed inside BIN folder.
I have this line of code,
WriteToFile(#"..\Config\Settings.xml")
The path used to write to a file Settings.xml inside Config folder inside the INSTALLATION DIRECTORY. However, now it tries to write to settings.xml inside Config folder at EXECUTION PATH.
So, if i execute my app from console as c:/users/guest/app.exe, it would try to interpret path relative to this location AND NOT relative to installation directory for the application.
Any help, suggestions?
Get the path of the executing assembly then add to it the folder and file name:
string pathOfExecutingAssembly = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string settingsPath = pathOfExecutingAssembly + "\\..\\Config\\Settings.xml"
Why don't you try getting the Executing Application's Path and append it before the path where you want to save
Path starting from \ means: start from the root directory on the current drive. \Config\Settings.xml executed from any C subdirectory gives: C:\Config\Settings.xml.
BTW, do you post exact code? It should be WriteToFile(#"\Config\Settings.xml") or WriteToFile("\Config\Settings.xml")
In any case, you need to decide, whether you want to search configuration file using absolute path, or path relative to current directory/installation directory/executable directory. The code, installation package and execution command should be changed accordingly.