How to use a custom downloads folder and the launcher executable path? - puppeteer-sharp

I want to download Chromium under a specific path. But to be able to then launch Chromium using an executable under that same path, I have to construct an executable path and that seems a little clumsy. I'm hoping there's a better way I'm currently missing. Here's the relevant version of my code basically:
string chromiumDownloadsPath =
String.Format(
#"{0}\Puppeteer Sharp downloads",
baseDirectoryPath
);
Downloader downloader = new Downloader(chromiumDownloadsPath);
await downloader.DownloadRevisionAsync(chromiumRevision);
var browser = await Puppeteer.LaunchAsync(
new LaunchOptions
{
Headless = true,
ExecutablePath =
String.Format(
#"{0}\Win32-{1}\chrome-win32\chrome.exe",
chromiumDownloadsPath,
chromiumRevision
)
},
chromiumRevision
);
What seems clumsy in particular is needing to add the "\Win32-{1}\chrome-win32" portion of the path. I was expecting Puppeteer.LaunchAsync to 'know' how to find an executable given a revision number because it 'should' already 'know' that I've directed Puppeteer Sharp to download the Chromium builds to a custom directory and I would think it has enough info to determine the platform too.

The Downloader class contains a method public string GetExecutablePath(int revision) that does exactly what I was expecting to be possible. The ExecutablePath property of the LaunchOptions object should thus be just:
ExecutablePath = downloader.GetExecutablePath(chromiumRevision)

Related

Access the Android Special Folder Path by using Environment

I want to save my logs to a folder which I can access with windows explorer. For example I want to create my log in the following path
This PC\Galaxy A5 (2017)\Phone\Android\data\MyApp\files
So I tried to use Environment variables... I get such as
/data/user/...
But here i cannot see the file what I created (using code I can access the path but I want to see in the explorer).
how I can create a path like above with code?
When I tried this code
var finalPath2 = Android.OS.Environment.GetExternalStoragePublicDirectory
(Android.OS.Environment.DataDirectory.AbsolutePath);
I get the path "/storage/emulated/0/data"
and
If i use the code
var logDirectory =Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.ApplicationData),"logs");
I get the following path like:
/data/user/0/MyApp/files/.config/logs
and
var logDirectory =Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.MyDocuments),"logs");
"/data/user/0/IM.OneApp.Presentation.Android/files/logs"
but unfortunately I cannot access this folder by explorer....
This PC\Galaxy A5 (2017)\Phone\Android\data\MyApp\files
So how to find out this path in c# by using environments?
Update:
when I give the following path hardcoded, it creates the file where I want..
logDirectory = "/storage/emulated/0/Android/data/MyApp/files/logs";
is there any environment to create this path? I can combine 2 environments and do some string processing in order to create this path. But maybe there is an easier way?
You are looking for the root of GetExternalFilesDir, just pass a null:
Example:
var externalAppPathNoSec = GetExternalFilesDir(string.Empty).Path;
Note: This is a Context-based instance method, you can access it via the Android application context, an Activity, etc... (see the link below to the Android Context docs)
Shared storage may not always be available, since removable media can be ejected by the user. Media state can be checked using Environment.getExternalStorageState(File).
There is no security enforced with these files. For example, any application holding Manifest.permission.WRITE_EXTERNAL_STORAGE can write to these files.
re: https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String)
string docFolder = Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.MyDocuments), "logs");
string libFolder = Path.Combine(docFolder, "/storage/emulated/0/Android/data/MyApp/files/logs");
if (!Directory.Exists(libFolder))
{
Directory.CreateDirectory(libFolder);
}
string destinationDatabasePath = Path.Combine(libFolder, "temp.db3");
db.Backup( destinationDatabasePath, "main");

Add download to Firefox via Firefox extension

This is my first Firefox extension which I'm developing with addon-builder [builder.addons.mozilla.org/] .
My question is simple but after trying many things, for many days, I'm unable to get results.
I want to know: How to add a file download to Firefox downloader??
I've a url like: http:// example.com/file.zip and a file location like: D:\myFolder.
I want to add this download via my firefox extension.
The things which I've searched are:
https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIWebBrowserPersist#saveURI%28%29
https://developer.mozilla.org/en-US/docs/Code_snippets/Downloading_Files
Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
const WebBrowserPersist = Components.Constructor("#mozilla.org/embedding/browser/nsWebBrowserPersist;1",
"nsIWebBrowserPersist");
var persist = WebBrowserPersist();
var targetFile = Services.dirsvc.get("Desk", Ci.nsIFile);
targetFile.append("file.bin");
// Obtain the privacy context of the browser window that the URL
// we are downloading comes from. If, and only if, the URL is not
// related to a window, null should be used instead.
var privacy = PrivateBrowsingUtils.privacyContextFromWindow(urlSourceWindow);
persist.persistFlags = persist.PERSIST_FLAGS_FROM_CACHE
| persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
persist.saveURI(uriToSave, null, null, null, "", targetFile, privacy);
Can you just gimme a start from where I should get the easiest possible download function.
Components.utils.import("resource://gre/modules/Services.jsm");
var {downloads}=Services;
downloads.addDownload(/*parameters*/); //see documentation for parameters.
Documentation for addDownload: nsIDownloadManager#addDownload()
Documentation and directory for the wide range of services provided by Services.jsm: Services.jsm

How to create the path to desktop for the target user..in C#.net

I am developing image extraction application in .net using C# in VS2010.
i have created a path ,where the image will be extracted.But this path is specific to my system.
string image1 = "c:\\Users\\Raghu\\Desktop\\r.bmp";
I want a path which should be general i.e when the project will be deployed ,the output file should be extracted in Target Users desktop.
how create a folder on desktop and and all my extracted files goes in it.
Any ideas! please help me!!
Next code will return path to the desktop of current user:
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
So, in your case it would be
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string image1 = System.IO.Path.Combine(desktop, "r.bmp");
Environment.SpecialFolder (http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx) contains many definitions of system folder paths. Take a look which you need.
You would use the DesktopDirectory for Environment.SpecialFolder. Something like this:
public static string GetDesktopDirectory()
{
return Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
}
Then using the result of that method, you can use Path.Combine to append a file name to it.
var myFilePath = Path.Combine(GetDesktopDirectory(), "r.bmp");
Path.Combine is the general solution for this, as directly concating strings may result in double slashes, etc. This takes care of that for you.

How to run "ipconfig" and get the output in adobe AIR in windows?

import flash.desktop.NativeProcess;
import flash.desktop.NativeProcessStartupInfo;
if (NativeProcess.isSupported) {
var npsi:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var processpath:File = File.applicationDirectory.resolvePath("MyApplication.whatever");
var process:NativeProcess = new NativeProcess();
npsi.executable = processpath;
process.start(npsi);
}
The above can only run a sub-application, but how to run an independent application(command) like ipconfig and get the result?
You in fact can scrape STDOUT and STDERR:
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onError);
process.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, inputProgressListener);
public function onError(event:ProgressEvent):void
{
trace(event);
trace(process.standardError.readUTFBytes(process.standardError.bytesAvailable));
}
public function inputProgressListener(event:ProgressEvent):void
{
process.closeInput();
}
public function onOutputData(event:ProgressEvent):void
{
trace(event);
trace(process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));
}
More info at: http://help.adobe.com/en_US/as3/dev/WSb2ba3b1aad8a27b060d22f991220f00ad8a-8000.html
And: http://www.as3offcuts.com/2010/08/air-2-native-process-example-mouse-screen-position/
Edit: realised maybe your question is also how to launch an external application? Here's an example how to run 'top' in OSX:
npsi.executable = new File("/usr/bin/top");
If you need network configuration information, you could use NetworkInfo.networkInfo.findInterfaces(). Will save you the trouble of interacting with another process and it's also portable.
http://help.adobe.com/en_US/air/reference/html/flash/net/NetworkInfo.html
If you want to run ipconfig.exe without knowing where it is located, you can run cmd.exe with arguments "/C" "ipconfig.exe ...". Of course, this requires to know where is cmd.exe. I had just included it with my AIR app (windows version).
I don't think you can, it's a self-impossed limitation of the NativeProcess API. However, you can create a little Windows binary yourself that calls ipconfig and passes the information back to the AIR app.
If creating that binary seems like a big deal, take a look to Haxe and xCross, you can get it done with a fairly similar language to ActionScript.

.NET Settings Relative Path

I am working on an application where I have an images folder relative to my application root. I want to be able to specify this relative path in the Properties -> Settings designer eg. "\Images\". The issue I am running into is in cases where the Environment.CurrentDirectory gets changed via an OpenFileDialog the relative path doesn't resolve to the right location. Is there a way to specifiy in the Settings file a path that will imply to always start from the application directory as opposed to the current directory? I know I can always dynamically concatenate the application path to the front of the relative path, but I would like my Settings property to be able to resolve itself.
As far as I know, there is no built-in functionality that will allow this type of path resolution. Your best option is to dynamically determine the applications executing directory and concatenate to it your images path. You don't want to use Environment.CurrentDirectory specifically for the reasons you mention - the current directory may not always be correct for this situation.
The safest code I've found to find the executing assembly location is this:
public string ExecutingAssemblyPath()
{
Assembly actualAssembly = Assembly.GetEntryAssembly();
if (this.actualAssembly == null)
{
actualAssembly = Assembly.GetCallingAssembly();
}
return actualAssembly.Location;
}
Are you looking for Application.ExecutablePath ? That should tell you where the application's executable is, remove the executable name, and then append your path to it.
2 options:
The code that uses the setting can resolve the setting against the directory of the current executing assembly.
You can create your own type that serializes as a string relative to the executing assembly, and has an accessor for the full path that will resolve against the directory of the current executing assembly.
Code sample:
string absolutePath = Settings.Default.ImagePath;
if(!Path.IsPathRooted(absolutePath))
{
string root = Assembly.GetEntryAssembly().Location;
root = Path.GetDirectoryName(root);
absolutePath = Path.Combine(root, absolutePath);
}
The nice thing about this code is that it allows a fully qualified path, or a relative path, in your settings. If you need the path to be relative to a different assembly, you can change which assembly's location you use - GetExecutingAssembly() will give you the location of the assembly with the code you're running, and GetCallingAssembly() would be good if you go with option 2.
This seem to work in both WinForms and ASP.NET (gives the path to the config file):
new System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile).Directory;
For Windows and Console applications, the obvious way is by using:
Application.StartupPath
I suggest you to use Assembly.CodeBase, as shown below:
public static string RealAssemblyFilePath()
{
string dllPath=Assembly.GetExecutingAssembly().CodeBase.Substring(8);
return dllPath;
}
You can try Application.ExecutablePath. But you need to make reference to System.Windows.Forms. This may not be a good idea if you want your class library to steer clear of forms and UI stuff.
You can try the Assembly.GetExecutingAssembly().Location. But if, somehow, you do a "Shadow Copy" before you run your application (like the default NUnit behavior), then this property will return you the shadow copy location, not the real, physical location.
The best way is to implement a function that calls the CodeBase property of Assembly object and chop off the irrelevant portion of the string.
I use the following two methods to help with that:
public static IEnumerable<DirectoryInfo> ParentDirs(this DirectoryInfo dir) {
while (dir != null) {
yield return dir;
dir = dir.Parent;
}
}
public static DirectoryInfo FindDataDir(string relpath, Assembly assembly) {
return new FileInfo((assembly).Location)
.Directory.ParentDirs()
.Select(dir => Path.Combine(dir.FullName + #"\", relpath))
.Where(Directory.Exists)
.Select(path => new DirectoryInfo(path))
.FirstOrDefault();
}
The reason to look at parent dirs to to be easier in use during development when various build scripts end up sticking things in directories like bin\x64\Release\NonsensePath\.

Resources