通过启动Chrome进程下载文件

通过启动Chrome进程下载文件

本文介绍了通过启动Chrome进程下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过启动一个新的Chrome浏览器来下载文件.我已经找到参数"--download",因此解决方案应该是"CHROMEPATH --download URI".( http://peter.sh/experiments/chromium-command-line-switches/#download )

I want to download a file by just starting a new Process of Chrome. I've found the parameter "--download", so the solution should be "CHROMEPATH --download URI". ( http://peter.sh/experiments/chromium-command-line-switches/#download )

我编写了代码以C#开始该过程,是的,我知道还有其他选择可以做到这一点,例如webclient,但我不想在我的代码中实现下载本身.

I wrote the code to start the process in C#, and yes, I know there are other options to do this like webclient, but I don't want to implement the download per se in my code.

string FILEURI = "example.org/file.png";
System.Diagnostics.Process prozess = new System.Diagnostics.Process();
prozess.StartInfo.FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
prozess.StartInfo.Arguments = "--download " + FILEURI;
prozess.Start();

这可以正常工作,只是打开链接"file://FILEURI".因此,如果没有任何用户交互,我将无法下载它.

This works without any problem, but just opening the link "file://FILEURI". So I can't download it without any user interaction.

推荐答案

Chrome始终会下载到下载文件夹中.您可以尝试获取最新的下载文件,如下所示:

Chrome always downloads to the download folder. You could try to just get the latest download file, like this:

new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"Downloads"))
    .GetFiles()
    .OrderByDescending(f => f.CreationTime)
    .First()

这存在风险(由DavidG概述)以及用户可能会下载其他文件等事实,因此这不一定是最佳方法.

This has risks (as outlined by DavidG) as well as the fact the user might download another file, etc. So this is not necessarily the best way.

这篇关于通过启动Chrome进程下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:28