3

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.

Sumeet Pujari
  • 181
  • 1
  • 5
  • 19

1 Answers1

0

use this

   string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\test.txt";
                    if (File.Exists(path))
                    {
                        Process.Start(new ProcessStartInfo(path));                            
                    }
Buzz
  • 6,030
  • 4
  • 33
  • 47
  • Thank you for the reply.I did add this code and it gave me no errors but when i published it it did not open the text file.I also made sure that the test.txt is included in resources should i make it public? More over i am hosting this application online does that make any difference? – Sumeet Pujari Oct 26 '12 at 12:16
  • Hey buzz The code you provided is unable to find the resource file.I am publishing the setup online to install.. It is for combining the files in the resource but unable to execute. If you wish to checkout http://setup.fastfixsupport.net/publish.htm – Sumeet Pujari Oct 26 '12 at 21:01
  • @sumeet: make sure your txt file is in the same directory , look into these questions http://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in and http://stackoverflow.com/questions/253468/whats-the-best-way-to-get-the-directory-from-which-an-assembly-is-executing – Buzz Oct 29 '12 at 04:26
  • Thanks buzz but i am still running into some dead ends just edited my first post. – Sumeet Pujari Oct 29 '12 at 10:44