Code a File Path that works in both OSX and Windows (dotnet core) - macos

I have to save files in my dotnet core application. I originally developed in windows; but when I ported the code to try developing on a mac, the file path is no longer valid (i.e. "c:\content..."). Is there a way to reference a file path, in my code, that will work in both situations? We deploy to azure... so the windows file path must there too.
FileInfo fileInfo = new FileInfo("c:\\Content\\SalesOrderOutput\\" +
fileName.txt);
FileStream stream = fileInfo.OpenWrite();

Use Path.Combine method like this:
internal static readonly FileInfo Mp4WithAudio = new FileInfo(Path.Combine(Environment.CurrentDirectory, "Resources", "input.mp4"));
Code from https://github.com/tomaszzmuda/Xabe.FFmpeg/blob/c8cc4232b5afa2860ede3be63a680d754ed73002/Xabe.FFmpeg.Test/Resources.cs

Related

Cannot find content file in bin folder

I have a IIS Express project running in debug mode in Visual Studio 2015.
The web application references a Class Library which has this line of code:
var certificate = new X509Certificate2(#"certkey.p12", "notasecret", X509KeyStorageFlags.Exportable);
The certkey.p12 file is in the same folder as the source code for the class library as is marked as "Build Action = Content" and "Copy if newer".
When I build, the file is copied to the web app's bin folder as expected. But when I call the method from the web app, it throws "file not found".
It works fine in Unit Testing where the file is copied to /bin/x64/Debug/
So where is IIS Express looking for the file? I suspect it is some sort of temp folder but why isn't VS copying the file there when I build/debug?
When running under IIS Express, it was looking for is "C:\Programs Files\IIS Express\certkey.p12".
I used a little peeking to find the folder that the file was actually copied to and came up with this:
string binPath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
Uri uri = new Uri(binPath);
string localPath = uri.LocalPath;
string dir = System.IO.Path.GetDirectoryName(localPath);
// now "dir" is the full local path of the bin folder
It is a little slow on first execute (~100ms) but faster on subsequent requests.
If there are any problems when I deploy this to production, I'll update.

nReco.Video Converter ffMpeg Error

I am using Nreco Video Converter for take video thumbnail on my MVC project. App is working correctly on local but it shows error on live host
Error is
Access to the path 'C:\Inetpub\vhosts********\httpdocs\bin\ffmpeg.exe' is denied.
I did search this file in my code and host bin folder but I can't found. NReco's site says
Simple and easy to use video conversion .NET library: all you need is one assembly (FFMpeg is embedded)
There is no ffmpeg.exe file in local and host bin folder or anywhere.
How can i fix this?
Thanks.
VideoConverter is a .net wrapper for ffmpeg tool (I'm an author of this library) and ffmpeg.exe is extracted into app bin folder (default location) on first use. On your live host asp.net process cannot write to app bin folder; this may be fixed by specifying another location, for example:
var ffmpeg = new FFMpegConverter();
ffmpeg.FFMpegToolPath = System.Web.HttpContext.Current.Server.MapPath("~/App_Data");

File Write - Unauthorized Access Exception

Trying to save a file locally from an app running in the iOS 8 Simulator and I'm continually getting access denied exceptions.
In previous apps I've used the following code to get a valid file path:
Environment.GetFolderPath(Environment.SpecialFolder.Personal)
or
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
But I've read that with iOS 8 this has now got to be written as:
NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory,
NSSearchPathDomain.User)[0]
So I'm using the following code to generate a file path for a .txt file and receiving an access denied exception when trying to save with it:
public void SaveMyFile(string content)
{
NSUrl[] urls;
string filePath;
//
urls = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory,
NSSearchPathDomain.User);
filePath = Path.Combine(urls[0].Path, "MyApp", "myFile.txt");
File.WriteAllText(filePath, content);
}
So the file path that it gives me and also denies access to is /Users/Idox/Library/Developer/CoreSimulator/Devices/92498E38-7D50-4081-8A64-83061DC00A86/data/Containers/Data/Application/C35B3E98-C9E3-4ABA-AA7F-CD8419FA0EA5/Documents/MyApp/myFile.txt.
I'm wondering if there's some setting that needs to be toggled to give the app write access to this directory or if the directory itself is invalid.
I've also done a call to Directory.Exists(string path) to check if the directory is there, which it is.
You're missing the Path property on urls[0].Path
filePath = Path.Combine(urls[0].Path, "MyApp", "myFile.txt");
This was fixed in Xamarin.iOS 8.4, so if you're using a recent version of Xamarin you can use Environment.GetFolderPath without problems (which is useful if you want to share code across platforms).

How to find files and folders in MAC volume using App sandboxed application

I am creating a sand-boxed application on MAC(10.9).When App Sand-boxed is off then i am able to get files and folders from any path. But when I does App Sand-boxed enabled it's not accessing any files and folders from any path.
Getting file and folder from path ("/Volumes/DriveName" or "/" etc.) when App Sand-boxed is off using Qt 5.0
// for folders
QStringList folderlist;
QDir curDir = QDir(path);
curDir.setFilter(QDir::Dirs);
QFileInfoList list = curDir.entryInfoList();
for(;<list.size;)
{
QfileInfo fileInfo = list.at(i);
folderList << fileInfo.filePath();
}
// for files
QStringList filelist;
QDirIterator dirIn(path,filterfiles,QDir::AllEnteries);
while(dirIn.hasnext())
{
dirIn.next();
QfileInfo fileInfo = dirIn.fileInfo();
filelist<< fileInfo.filePath();
}
What i do same when App sand-boxed is enabled?
That's the point of a sandbox, you can only access resources within the sandbox. A user may add files to the sandbox, but that requires your application to launch a file open dialog box, so they can choose the file they want to work with.
I suggest you start by reading the Apple documentation here.

Use relative path in Firefox extension

I develop Firefox extension with bundled executable file which should be run on browser startup.
To run process I need get nsIFile or nsILocalFile instance which points to executable file.
I know one solution how to get it using directory service:
var file = Components.classes["#mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
file.append("extensions");
file.append("<extension id>");
file.append("<relative path>");
But this solution has two disadvantages:
It doesn't work in development mode, when instead of installed extension I have only text file with real extension path
I'm not sure that it will work on all Firefox configurations because of hardcoded "extensions" part of the path
So is there any nicer way to run executable file which comes with Firefox extension?
Thanks.
You are making way too many assumptions about the directory structure of the Firefox profile - don't. The Add-on Manager API lets you get the path of a file inside the extension, you should use it:
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("<extension id>", function(addon)
{
var uri = addon.getResourceURI("<relative path>");
var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file;
...
});
A restartless addon's startup function (in the bootstrap.js file) will receive, as its first parameter, the path where the addon is installed. You can then play various tricks to read files inside the .jar file, if any: see https://github.com/protz/GMail-Conversation-View/blob/master/bootstrap.js#L55 as an example.
In a non-restartless case, I must confess I don't have much of an idea :).
I found this thread looking for a way to reference a path to an image hosted in extension's directory from a content script. Here's a solution:
Include your files in web_accessible_resources in the extension's manifest.
"web_accessible_resources": [
"images/*"
]
Absolute paths to these resources contain randomly generated UUID, therefore we're using runtime.getUrl() giving it the path relative to manifest.json. Example:
let myImg = document.createElement('img');
myImg.src = browser.runtime.getURL("images/my-img.png")

Resources