Can't reference json file in root - json

I created a new Azure WebJobs project which is a console app. I placed a settings.json file in the root and I'm trying to access it using the following code but I keep getting an error that says it cannot locate the file. I think it's looking for it under Debug folder but I don't want to move the file there. How do I reference that file?
var config = new Configuration();
config.AddJsonFile("settings.json");
I tried "~/settings.json" but that didn't work either.

You need to identify if it's a deployment or runtime issue, per this article.
Make sure that your file is in fact getting deployed:
In VS, check that it has Copy to Output directory set to Copy if Newer
Use Kudu Console to look at the relevant WebJob folder under D:\home\site\wwwroot\App_Data\jobs\... and make sure that the json file made it to there next to the exe.

You can try to add your json file into your WebJob project's Resources as shown:
Remember to set the file type as Text and encoding to UTF-8.
In your code, you can easily access your json file as string as below:
// The Resources property depends on your actual file name being referenced
var settingsJson = Resources.settings;
Hope this helps!

Related

Read and Write file using vs code extension

i am building an extension to parse json using vs code extension.
so my need is ,it should be able able to load .json file from a particular folder and iterate through content of the file.
Then it should allow user to select few keys from it make a new json file out of this and save it in any folder.
But i am not able to find any way to read and write files in "vs code extension".Could someone please help me.
If you want to read the current edit state of a file you can use the following API workspace function:
vscode.workspace.openTextDocument(uri).then((document) => {
let text = document.getText();
});
This will show you the current state of the file including unpersisted changes. document is of type TextDocument and has isDirty set to true if it has pending changes.
Since the extension runs in nodejs, you should be able to use any nodejs module built-in or installed by npm in the usual way.
For your purpose you will be OK with the built-in fs module: https://nodejs.org/dist/latest-v6.x/docs/api/fs.html
In your extension you will need to import the required module, so your code file should contain this:
let fs = require("fs");
and then use the methods in the usual way, eg. fs.fileReadSync( filename, encoding ) ...
Please not that there is one exception. If you install a nodejs module containing compiled, binary code, it will not run in the extension and instead you will see an error message saying something like %1 is not a valid Win32 application. Pure javascript modules are OK, though.
VSCode extensions are running in node.js. Therefore you can use any available node.js package/module within your extension. For instance, check out this question for reading JSON.
For JSON, you just need to require or import the JSON file, such as:
const jsonObject = require('./myJSONfile.json');
// do something
For JSON with comments, you can use node-jsonc-parser.
After the manipulation, you could use the fs module of nodej.js to write to the disk.

chrome.filesystem: Rename a file and save it on the client's disk

How could I do with chrome.filesystem to rename a file and save it. For example, if my file is named myfile.txt I would rename the myfile.html and save it without using the saveAs function. And if that's not possible, do I have a solution.
The problem is that I have to save the file on the client's disk. So for me to use the filesystem functions can not be a solution, I have not seen that chrome.filesystem API that allows.
Thank you in advance! I'm a little discouraged. I also watched the browserify aside to work around the problem, but I have not found how to do it.
To rename a file you must have the ability to create a new file on the user's filesystem. You can get this permission by asking the user to open the whole directory in which the project exists. Then you can create any file you want within that directory by calling getFile with { create: true } on the resulting DirectoryEntry.
Edit: See this example for duplicating files selected by the user. Instead of using fs.root as is done here you can use the result of chrome.fileSystem.chooseEntry as the DirectoryEntry in which the file is saved.

Reading files with Node.js from input file type selector

I am trying to select files using input file type and then upload them to dropbox using the dropbox Core API or saving it to a local folder using Node.JS readFile and writeFile methods. The problem is that most of these methods require the file path and all I have is the name of the file that is stored in the File object array and for what I have read browsers do not allow to get the full path for security reasons. I don't know how to go about this, can anyone help me solve this? Thanks!!

Referring to a file with partially known name in html

I am making a webpage. I have to give link of a log file which is present on the server but that file is generated with a random number at the end.
I know the starting part of the filename. Here is my code -
Download log file Here
for example the name of the file is "log_parser2088.log" this 2088 number is randomly generated everytime the code runs and there is only one file in that folder which starts with the name "log_parser". I want to give reference to this file but this is not working.
HTML doesn't support this kind of references. Use PHP or ASP.NET for this task since these server side programms have access to the servers file system and can, f.e., read every filename in a directory.
Example for PHP:
// path to your log file directory
$directoryPath = "\\home\\logs\\";
// read all files from that directory
$files = scandir($directoryPath);
// print download link
foreach($path in $files)
echo("Download");

How to load a local directory in as3

I want to load a directory in actionscript. To do that I use the File class but sadly it doesn't work.
Here is what I did. First I created the directory in the applicationStorageDirectory using this code:
var root:File = File.applicationStorageDirectory.resolvePath(UIDUtil.createUID());
root.createDirectory();
After that I dispatch an event so that the application copies some files into that folder. To do that I use the method File.copyToAsync (that works, so this is not the problem). When all files are copied to that directory I try to load that directory using the File call again. The reason I want to do that is, that I need the content/data as a byte array. The data property of the File class is only set when the load method is called successfully before.
Since the directory is in the application storage directory (the url property of the file object has this content: app-storage:/FF011DBC-2E92-46A8-D5F8-29FE1DD8FA7A) I thought just calling the load method would work. I was wrong... Although the documentation of that method says I can just call that method when the application runs in AIR and the file to load is inside the application sandbox, I get the following error:
Error #2044: Unhandled IOErrorEvent:. text=Error #2038: File I/O Error.
at flash.filesystem::File/resolveComponents()
at flash.filesystem::File/resolvePath()
...
Do you have any ideas what could be wrong? Is it even possible to load a directory using the File class? Or does that error occur because the isDirectory flag is set to true in the file object?
kind regards
Markus
I believe a File, when pointing to a directory, does not contains the data of the files contained inside it. You would have to use File.getDirectoryListing() to loop over all files the folder, and append their bytes to the ZIP archive separately. Don't forget to check if you come across sub-directory...
You can check this link for an example of how to do this.