I was wondering if it was possible to download a file to a specific location then run it.
In this code I try to run from resource. But you may first download to your specific folder
then run from that path
In your application project, go to Properties/Resources.
Click on Add Resource.
Select "Add Existing File.."
Browse to the .exe you want to embed, select it and click "Open".
If you want to change the resource name:
In the Resource Editor, right-click the exe you added and select "rename" and enter an appropriate name.
In this example, I called it "MyTestExe".
Where you want to extract and run the executable, add code like this:
private void button_Click(object sender, EventArgs e)
{
byte[] exeBytes = Properties.Resources.MyTestExe;
string exeToRun = #"C:\TEST\MyTestExe.exe";
using (FileStream exeFile = new FileStream(exeToRun, FileMode.CreateNew))
{
exeFile.Write(exeBytes, 0, exeBytes.Length);
}
using (Process exeProcess = Process.Start(exeToRun))
{
exeProcess.WaitForExit();
}
}
Related
I have a CHM file and a help menu and I want to add that file to resources but when I added it to resources it does not work.
I tried to add to subfolder in Resources but still no use
void HelpToolStripMenuItemClick(object sender, EventArgs e)
{
string filePath = Directory.GetCurrentDirectory() + "\\Help\\abc.chm";
try
{
//Check if already exists before making
if (!File.Exists(filePath))
{
var data = Properties.Resources.abc.chm;
using (var stream = new FileStream("abc.chm", FileMode.Create))
{
stream.Write(data, 0, data.Count());
stream.Flush();
}
MessageBox.Show("file made");
}
}
catch
{
//May already be opened
}
Help.ShowHelp(this, filePath);
}
I want to work even when this to work even when the setup is installed
on any computer
I would be better if any tells how to embedded in my setup
First of all, add the help file to your project and open the Properties window for that file. In the CopyToOutputDirectory, choose ‘Copy always’ or ‘Copy if newer’.
This will make sure, that when you’re debugging/testing your application, that it will copy the file to the bin folder.
Start your project in Debug mode and check your bin/debug output folder. Do the same for Release mode and output folder. The CHM should reside there and gets included in your deployment.
A sample code snippet for calling CHM's:
private const string sHTMLHelpFileName = "CHM-example.chm";
...
private void button1_Click(object sender, EventArgs e) {
System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath + #"\" + sHTMLHelpFileName);
}
For download I provide a C# VS2008 Project including the code above and the help files with different help viewer windows (different CHM files for show case only).
I wanted to start a .exe file in a different folder but I wanted other people to also use it and I've been trying many things but it just keeps opening the file that the program that I'm creating is in. (I'm new to c#).
My ex of ^: \Desktop\VSCheatDetector\CheatDetector.exe(the program) and another regular file named viper_screenshare_tool and it has CheatDetector.exe (which I want to open when I click a certain button)
Code:
private void cheat_smasher_click(object sender, EventArgs e)
{
string dir = AppDomain.CurrentDomain.BaseDirectory;
Process.Start(dir, "vipers_screenshare_tool\\CheatDetector.exe");
}
You don't want to use AppDomain.CurrentDomain.BaseDirectory; - I'd suggest using App.config or something like that instead.
I have this chunk of code:
private void button4_MouseEnter(object sender, MouseEventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"Resources/navigation.wav");
player.Play();
}
And I get FileNotFoundException, but navigation.wav is in Project/Resources. Plese help!!!
This looks for the file from your Bin\Debug Folder
You have couple of options:
Right click the file and pick Properties. Select for BuildAction = Content.
You will find the file under Bin\Debug\Resource\Sound.wav
Right click the file and pick Properties. Select for BuildAction = Embedded Resource.
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "NamespaceName.FolderName.Sound.wav";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
var wave = new WaveFileReader(stream);
Console.WriteLine(wave.TotalTime);
}
The path is determined relatively from the executable, so in this case probably Bin\Debug.
Try to add the resource in your application as Content (it copies the file to Bin\Debug). That should work.
That path is relative to the directory in which the application is currently running. If you hit F5 in Visual Studio this is most probably bin/Debug, so the file should be there.
Consider embedding this resource, or setting Copy to output directory property to "copy always".
To start with, you need a backslash: #"Resources\navigation.wav"
If this doesn't help, then most likely you are running your application from a different directory than you think. Are you running in debug mode from VS? Is your file in Project\bin\Debug\Resources then?
In WPF application where I have included some files in resources, I want to execute them on a button click. How do I specify a path in Process.Start().
private void button1_Click_2(object sender, RoutedEventArgs e)
{
Process.Start("test.txt");
}
Or is there any other way?
private void button1_Click_2(object sender, RoutedEventArgs e)
{
string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\test.txt";
if (File.Exists(path))
{
Process.Start(new ProcessStartInfo(path));
}
else
{
MessageBox.Show("No file found"+path);
}
I added a message box and it showed No files found. :(
EDIT:
I Tried to check the path after publishing and this what i got.
No File Found With a Path - C:\Users\Administrator\AppData\Local\Apps\2.0... test.txt
Before I published the Application I got a path which id
No File Found at ..project..\bin\Debug\test.txt which is obvious since my Resource file not included there its Under a Resource Folder and not Debug when i add a test file in debug it open without any problem.
Can someone Help throwing some light on this case.
EDIT:
I want to open a file from Resource directory # C:\Users\Administrator\Documents\Visual Studio 2010\Projects\FastFix\FastFix\Resources Which would be included in my project when i am going to publish it is going to run as a standalone application without installation.
use this
string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\test.txt";
if (File.Exists(path))
{
Process.Start(new ProcessStartInfo(path));
}
If i created a button named Browse ,,If i click Browse button i have to
browse my system folders .Can any one give me the required code to browse
the specific folders
Check out the FolderBrowserDialog if you are wanting to find a folder.
If you are wanting to open a file, you can use the OpenFileDialog.
Both links provide examples of how to use the dialogs.
This MSDN link provides how to get the special system folders. And you can specify the type of special folder you want by using the appropriate enumeration. Check this link for those.
Essentially, you are going to do something like so if you want to pop up a dialog and browse to the System folder and select some files from there:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog od = new OpenFileDialog();
od.InitialDirectory = Environgment.SpecialFolder.System;
od.Multiselect = true;
if (od.ShowDialog() == DialogResult.OK)
{
// do stuff
// od.Filenames will hold the string[] of selected files
}
}
Assuming you want to display the results in a list named files something like:
String directory = Environment.GetFolderPath (Environment.SpecialFolder.System);
String[]files = Directory.GetFiles (directory);
foreach (String file in files)
files.Add (file);
You can use a FolderBrowserDialog control and call the code there if you want to browse multiple directories.