I currently have an Awesomium Webvcontrol in my WinForms application. When I click a download link it prompts with a save file dialog.
I need it to download the file to a preset location and automatically open it. I am using the latest version of Awesomium.
References:
using Awesomium.Windows.Forms;
using Awesomium.Core;
Has anyone an idea how to make the control point to a preset location?
I managed to figure a way around this.
I added an method to the download event of webcore.
Awesomium.Core.WebCore.Download += onDownload;
The method looks like this.
public static void onDownload(Object sender, DownloadEventArgs e)
{
e.Handled = true;
using (WebClient Client = new WebClient())
{
FileInfo file = new FileInfo("Your Path");
//replace Your Path with the path you wish to save the file including filename and extension
Client.DownloadFile(e.Url.ToString(), file.FullName);
//System.Windows.Forms.MessageBox.Show("Downloaded!");
Process.Start(file.FullName);
}
}
This now downloads and opens the file. In my case it was a .exe application.
Related
I am trying to open a PDF file which is a resource file embedded within my WPF application.
The resource file is marked as a Resource for build action and also as do not copy. I am doing below without success. I am not able to build the Uri based on the embedded resource file:
System.Windows.Controls.Frame frame = new System.Windows.Controls.Frame();
System.Windows.Controls.WebBrowser browser = new System.Windows.Controls.WebBrowser();
browser.Unloaded += Browser_Unloaded;
Uri myUri = new Uri("pack://application:,,,/myApp;component/Resources/myPDFFile.pdf", UriKind.RelativeOrAbsolute);
browser.Navigate(myUri);
frame.Content = browser;
And this is my method when PDF is unloaded:
private void Browser_Unloaded(object sender, System.Windows.RoutedEventArgs e)
{
browser = frame.Content as System.Windows.Controls.WebBrowser;
browser.Dispose();
frame.Content = null;
}
Instead of a resource file embedded within the application, if I put the PDF somewhere in the disk and I specify the absolute path as "C:\Users\MyUser\Downloads\myPDFFile.pdf", then it works but I want the PDF file to be embedded within the application as a resource file and be able to open it.
It looks like the URI I have built is not correct, sure is failing pack://application:,,,/myApp;component/Resources/myPDFFile.pdf but I am not able to see why.
I have a PDF file that I would like to load with a button click. I can reference the file in debug mode, but when I publish the project, the pdf doesn't migrate over or 'install' with the project.
The file is located in Resources/file.pdf
In the WPF form, I call the "OpenFile_Click" function on click.
Here is my function:
private void OpenFile_Click(object sender, RoutedEventArgs e)
{
string appPath = AppDomain.CurrentDomain.BaseDirectory;
Process.Start(appPath + "Resources/file.pdf");
}
This clearly doesn't work to open that file. I can add ../../ in front of the Resources folder and it will open in debug, but that isn't very helpful.
So, what is the best option for opening an external file like a PDF?
After setting Properties\Copy to Output Directory = Copy if Newer as suggested by Clemens, I would also recommend the use of System.IO.Path.Combine to ensure the correct path delimiter for the platform.
If there is still an error when invoking the Process.Start then try starting "explorer.exe" with the combined file name. I successfully tested the following, see if you can repro.
private void OpenFile_Click(object sender, RoutedEventArgs e)
{
var filePath = System.IO.Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"Resources",
"file.pdf"
);
Process.Start("explorer.exe", filePath);
}
I have a problem Where I cant make my program automatically read the given file path inside the .dat and be ready to launch the program when pressing launch file without opening openFileDialog and choosing the program every time.
the code im using here is for the user to enter the file path for the first time then create a file path .dat file and it works with now issues.
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string path = Path.Combine(desktop, "LS\\Fail-SafePath.dat");
openFileDialog.InitialDirectory = filePath;
openFileDialog.Filter = " PlayGTAV (*.exe)|PlayGTAV.exe";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filePath = openFileDialog.FileName;
var fileStream = openFileDialog.OpenFile();
using (StreamReader reader = new StreamReader(fileStream))
{
fileContent = reader.ReadToEnd();
}
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(filePath);
}
After that that i have a start button for it
private void panel21_MouseClick(object sender, MouseEventArgs e)
{
Process.Start(filePath);
}
This works well when the user does it for the first time but now I want it to read that .dat file path automatically without having to ask the user for the file path every single time which I don't know how to do and need help with please.
I was thinking to do it like that: When Pressing the Launch button (After the first time) The Program Checks if the Fail-SafePath.dat Exists if Yes it reads the lines from it and starts the program from the given path without opening OpenFileDialog.
I'm Using Visual Studio, Windows Form.
If it's a file that the application will always need, then something like you mentioned:
I was thinking to do it like that: When Pressing the Launch button (After the first time) The Program Checks if the Fail-SafePath.dat Exists if Yes it reads the lines from it and starts the program from the given path without opening OpenFileDialog.
Could work easily enough. You could have your application look for it in the default location, and if not there, have your user select it.
Another solution could be using something like Application Settings or User Settings, which are values persisted between executions of .NET projects.
Depending on your full application design, you could also have the file path and other settings stored in some database or other data storage. There are a lot of ways to accomplish this.
EDIT: To elaborate further on the Application Settings
The application settings are very easy to read and write to.
You just need to create the ones you want, before trying to use them.
They can be created by:
Open Visual Studio.
In Solution Explorer, expand the Properties node of your project.
Double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings.
In the Settings designer, set the Name, Value, Type, and Scope for your setting. Each row represents a single setting.
To read from your settings:
this.FilePath= Properties.Settings.Default.FilePath;
To write to and save the setting:
Properties.Settings.Default.FilePath= Path.GetFullPath("importantFilePath");
Properties.Settings.Default.Save();
I am relatively new to C#/Visual Studio 2015 application development, coming from Android. I am writing a chat application that also allows users to send files to each other. The file transfer functionality is in place; the file gets downloaded to a pre-set folder when received, and the file-path of that file is then shown in the chat box to the recipient. However, that file path is shown as though it were regular text.
How do I make it such that said file-path (and/or urls, ideally) appear as a clickable hyperlinks, that then open said file?
Any help or resources to be pointed to would be most appreciated!
If you create a linkLabel object to show the path, you can add a callback to the event LinkClicked and open a file explorer:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("C:/");
}
Here we go:
You should use Uri class to build your url from string:
string filePath = "C:\\example.txt";
Uri uri = new Uri(filePath);
return uri.AbsoluteUri;
Hope it helps;)
I'm trying to build a simple program and I want to find a way to embed a file (or multiple files) in the executable.
The program is very simple. I will be building a form using C# in visual studio. On the form, there will be couple questions and a submit button.
Once the user has answer all the questions and hit the submit button, if all answers are correct, I want to give the user the file as a prize. (The file can be image, video, or a zip file that contains multiple other files)
The way I want to give the user the file is very flexible. It can just be creating this file in the same directory as the executable, or given the download option for the user to save it somewhere else.
Below is the pseudo code
private void submit_Click(object sender, EventArgs e)
{
//functions to check all answers
if(all answers are correct)
{
label.Text = "Congrats! You answered all questions correctly";
//create the file that was embeded into the same directory as the executable
//let's call the file 'prize.img'
Process.Start("prize.img");
}
else
label.Text = "Some answers were not correct";
}
The logic is pretty simple and straight forward. The problem is, how can I embed "prize.img" into the executable? I will be giving this program (.exe) to a friend so he will not have any source and I can't guarantee the path.
You want to do embed the file as a resource.
Right-click the project file, select Properties.
In the window that opens, go to the Resources tab, and if it has just a blue link in the middle of the tab-page, click it, to create a new resource.
In your code you can type in Resources.TheNameYouGaveTheFileHere and you can access its contents. Note that the first time you use the Resources class in a class, you need to add a using directive (hit Ctrl+. after typing Resources to get the menu to get VS to do it for you).
Do you need help with the saving of the file also?
Edit:
You could do something like this:
var resource = Properties.Resources.yourResource;
FileStream fileStream = new FileStream("filename.exe", FileMode.CreateNew);
for (int i = 0; i < resource.Length; i++)
fileStream.WriteByte((byte)resource[i]);
fileStream.Close();
Does it help you?
EDIT:
As I can see you are getting a stream, here is an update to make it work:
var resource = Properties.Resources.yourResource;
FileStream fileStream = new FileStream("filename.exe", FileMode.CreateNew);
resource.CopyTo(fileStream);
fileStream.Close();
Does it work now?
Can't you add the image file as a resource in your solution? And then you can reference that resource in your code?
See if this may be what you're looking for:
Embedding Image Resource in Project
The link provided offers step-by-step how to accomplish this:
After adding the image as a resource, the instructions show how to access your image resource and use it in a program. Just click the link above and follow the instructions.
I followed the instructions above and wrote my program like this:
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var myAssembly = Assembly.GetExecutingAssembly();
var myStream = myAssembly.GetManifestResourceStream("WindowsFormsApplication6.Desert.jpg");
var image = new Bitmap(myStream);
pictureBox1.Image = image;
}
}
}
My output looks like this:
The image on the form came from the image that I embedded as a resource. Very easy to do.
Add the file into project then set its Build Action as Embeded resource.
From your code you can do something like this:
var _assembly = Assembly.GetExecutingAssembly();
var _stream = _assembly.GetManifestResourceStream("namespace.fileneme");