Execute files included in Resource folder - c#

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));
}

Related

Run a exe thats on a webserver c# console app

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();
}
}

Open CHM File from Resources c#

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).

Starting a .exe file but won't work?

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.

How to display chm file in c# winforms application

I have added .chm file to my application root. when i fetch the file using below code it is referencing the path to bin/release/somehting.chm
System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath+"\\"+"somehting.chm");
i want to get the path relative to installation location of application. please help.
the chm file added to the root directory is not loading after deploying the application. its not even loading while debugging in visual studio and not giving any error.
As I can see the first code snippet from your question calling Help.ShowHelp isn't so bad. Sometimes I'm using the related code below. Many solutions are possible ...
Please note, typos e.g. somehting.chm are disturbing in code snippets.
private const string sHTMLHelpFileName = "CHM-example.chm";
...
private void button1_Click(object sender, EventArgs e) {
System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath + #"\" + sHTMLHelpFileName);
}
So, please open Visual Studio - Solution Explorer and check the properties of your CHM file. Go to the dropdown box shown in the snapshot below and set "Always copy" (here only German). 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 I hope your CHM call works.
You need :
String exeDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
So :
String HelpFilepath = "file://" + Path.Combine(exeDirectory , "somehting.chm");
Help.ShowHelp(this, path);
Answer from similar topic is:
// get full path to your startup EXE
string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
// get directory of your EXE file
string exeDir = Path.GetDirectoryName(exeFile);
// and open Help
System.Windows.Forms.Help.ShowHelp(this, exeDir+"\\"+"somehting.chm");

Program fails to load image from relative path on drag-drop file open

I have a program I have written in C#, which loads an image with Image.FromFile, and it loads the image successfully every time. However, when you drag and drop another file on the executable, like you are giving the program the command line argument of the file, and the file is not in the same folder as the executable, the program crashes because it says the path to the file does not exist, even though it does.
I think that by dropping a file on the executable, it's changing the path it's loading images from somehow. How can I fix this problem?
Your program would be started with a different Environment.CurrentDirectory. Always make sure you load files with an absolute path name (i.e. don't use Image.FromFile("blah.jpg")).
To get the absolute path to a file that's stored in the same directory as your EXE, you could use Application.StartupPath for example. Or Assembly.GetEntryAssembly().Location if you don't use Windows Forms.
It depends on how you are initiating the file drag outside of your application. If you click and drag a file from Windows Explorer, the full absolute path name is included in the drop. In this case the following code shows the filename and performs a drop of the file's contents into a textbox:
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var objPaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
if (objPaths != null && objPaths.Length > 0 && File.Exists(objPaths[0]))
{
MessageBox.Show(string.Format("Filename: {0}", objPaths[0]));
using (TextReader tr = new StreamReader(objPaths[0]))
textBox1.Text = tr.ReadToEnd();
}
}
}
So let us know more about your drag source. Most likely you will have to modify your source to drag the absolute path, or somehow determine the full path from the relative path in the drag data.
Also, your program should never crash due to bad data. Either check for the required conditions, or use a try/catch block around the necessary code.

Categories

Resources