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.
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'm trying to find a file in a settings folder in my application. I have a xml file there. When I run the following code:
XDocument xDoc = XDocument.Load(#"..\settings\Settings.xml");
I get the DirectoryNotFoundException and the exception says not found at \bin\settings\Settings.xml'., instead of above. I even tried the full root directory to see the issue, C://... but it still includes a bin folder?
How can I have it so it doesn't include the bin part?
By default, the build results in Visual Studio are saved in a folder like bin\Debug. Since you use a relative path that jumps one folder higher, you get yourProjectFolder\bin\settings\Settings.xml. That file doesn't exist, since it's presumably in the project folder, not the bin folder.
The typical way to deal with this is to make sure the files that are supposed to be a part of the content actually have Build Action set to Content.
Using a rooted path definitely works - most likely, you made a mistake somewhere; either the path isn't rooted at all, or you're doing something like interpreting the path as an URI rather than a file path. XDocument.Load takes a URI, not a file path - the proper way to reference an absolute path on the filesystem would be file://C:/ThePath/Settings/Settings.xml.
I have a xml file which is in a folder in the solution. I tried to access it using Server.MapPath. It was working fine in a aspx page of a different project. When I tried to access the file in my class library project, I am not supposed to use Server.MapPath. So I tried with HttpContext.Current.Server.MapPath. Problem is this class library project is calling from a separate WCF service project, so current server is WCF service project's server. So it ended up with error path is not valid.
This is what I tried- HttpContext.Current.Server.MapPath("./folder/conf.xml.config")
Any solution?
System.Web is already imported.
You can create a virtual-directory inside the web-services. This virtual-directory will point to Physical location of "folder/conf.xml.config".
Once this is done, you can access it using your existing code like below...
Server.MapPath("folder/conf.xml.config")
Server.MapPath works only with files that are inside the website and is used by specifying a relative location:
string configFile = Server.MapPath("~/App_Data/config.xml.config");
If you want to access a file from some other location you will have to manually provide the absolute path to it:
string configFile = #"c:\work\some_folder\config.xml.config";
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 am using a webservice to accept a URL of an already uploaded file
and want to copy this file (which is in one server with public access) to another server folder
issue is that when Server.Mappath is used, it is always referring to the web Service project location and not to the URL's location.
Is there anyway that I can copy the file using webservice with the URL only?
if the file is located in a different web site than the webservice, then you have to tell your webservice where are the files located. You could put a key in web.config for this location and then compose the filename:
Path.Combine(ConfigurationManager.AppSettings["FilesPath"],fileName);
Be aware that you have to have access rights to that folder.