Relative path

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 17844 Views - Last Post: 08 July 2010 - 08:24 AM Rate Topic: -----

#1 TomVdV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 28-May 09

Relative path

Post icon  Posted 01 June 2009 - 02:13 AM

Hi all,

a studid question.

I've include a pdf-file into my project.

It works when I'm working with the full path
"C:\\C-Sharp\\Voetbaladministratie\\Opstelling.pdf"


But I get an error on runtime when I'm trying to use a relative path.

I've tried the following thing:
"Opstelling.pdf"

"/Opstelling.pdf"

".../Opstelling.pdf"


but they didn't works.
Can somebody help me?

This post has been edited by TomVdV: 01 June 2009 - 03:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Relative path

#2 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Relative path

Posted 01 June 2009 - 02:41 AM

View PostTomVdV, on 1 Jun, 2009 - 01:13 AM, said:

Hi all,

a studid question.

I've include a pdf-file into my project.

It works when I'm working with the full path
<code>"C:\\C-Sharp\\Voetbaladministratie\\Opstelling.pdf"</code>

But I get an error on runtime when I'm trying to use a relative path.

I've tried the following thing:
<code>"Opstelling.pdf"</code>
<code>"/Opstelling.pdf"</code>
<code>".../Opstelling.pdf"</code>

but they didn't works.
Can somebody help me?


Try to search for file by this path:

string fn = System.Windows.Forms.Application.StartupPath + "\\Opstelling.pdf";


~'J'~
Was This Post Helpful? 0
  • +
  • -

#3 TomVdV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 28-May 09

Re: Relative path

Posted 01 June 2009 - 03:58 AM

string fn = System.Windows.Forms.Application.StartupPath + "\\Opstelling.pdf";
Messagebox.Show(fn);
//Process.Start("AcroRd32.exe","C:\\C-Sharp\\Voetbaladministratie\\Opstelling.pdf";
Process.Start("AcroRd32.exe, "fn");



The Messagebox shows me C:\C-Sharp\Voetbaladministratie\bin\Debug\Opstelling.pdf

Acrobat start up and give then an error: "There was an error opening this document. This file cannot be found"
Was This Post Helpful? 0
  • +
  • -

#4 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Relative path

Posted 01 June 2009 - 04:02 AM

for path use
String _Path = @"C:\directory\blabla\file.etc";

Was This Post Helpful? 0
  • +
  • -

#5 TomVdV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 28-May 09

Re: Relative path

Posted 01 June 2009 - 04:16 AM

I have tried to add a @ before "fn", but without succes

string fn = System.Windows.Forms.Application.StartupPath + "\\Opstelling.pdf";
Messagebox.Show(fn);
//Process.Start("AcroRd32.exe","C:\\C-Sharp\\Voetbaladministratie\\Opstelling.pdf";
Process.Start("AcroRd32.exe, @"fn");

Was This Post Helpful? 0
  • +
  • -

#6 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Relative path

Posted 01 June 2009 - 04:30 AM

try this it is working on my computer
String _Path = @"C:\mypdf.pdf";
			System.Diagnostics.Process.Start("Acrobat.exe", _Path);

Note: Name of Process or Application Depend on it's Version.

This post has been edited by noorahmad: 01 June 2009 - 04:32 AM

Was This Post Helpful? 0
  • +
  • -

#7 TomVdV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 28-May 09

Re: Relative path

Posted 01 June 2009 - 04:37 AM

it the same as
Process.Start("AcroRd32.exe","C:\\C-Sharp\\Voetbaladministratie\\Opstelling.pdf";


That also works for me.

But I want to use a relative path, so I can move the application to other directories and other PC's without changing al paths.
Was This Post Helpful? 0
  • +
  • -

#8 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Relative path

Posted 01 June 2009 - 04:40 AM

for opening a file use OpenFileDialog and assign it to Proocess.
Was This Post Helpful? 0
  • +
  • -

#9 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Relative path

Posted 01 June 2009 - 06:14 AM

View PostTomVdV, on 1 Jun, 2009 - 03:37 AM, said:

it the same as
Process.Start("AcroRd32.exe","C:\\C-Sharp\\Voetbaladministratie\\Opstelling.pdf";


That also works for me.

But I want to use a relative path, so I can move the application to other directories and other PC's without changing al paths.


Sorry I don't see where is your file was stored
Usually I use something like this

Add
using System.IO to reference scope

Then let you try:

   string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
				string strFileName = Path.Combine(execPath, "Opstelling.pdf");


~'J'~
Was This Post Helpful? 0
  • +
  • -

#10 TomVdV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 28-May 09

Re: Relative path

Posted 01 June 2009 - 07:41 AM

View Postnoorahmad, on 1 Jun, 2009 - 11:40 AM, said:

for opening a file use OpenFileDialog and assign it to Proocess.


I've tried this
			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			openFileDialog1.InitialDirectory = "c:\\";
			openFileDialog1.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
			openFileDialog1.FilterIndex = 2;
			openFileDialog1.RestoreDirectory = true;

			MessageBox.Show(openFileDialog1.FileName);
But when I run the application I didn't get an dialogbox, I only receive an empty messagebox
Was This Post Helpful? 0
  • +
  • -

#11 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Relative path

Posted 01 June 2009 - 07:57 AM

View PostTomVdV, on 1 Jun, 2009 - 08:41 AM, said:

View Postnoorahmad, on 1 Jun, 2009 - 11:40 AM, said:

for opening a file use OpenFileDialog and assign it to Proocess.


I've tried this
			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			openFileDialog1.InitialDirectory = "c:\\";
			openFileDialog1.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
			openFileDialog1.FilterIndex = 2;
			openFileDialog1.RestoreDirectory = true;

			MessageBox.Show(openFileDialog1.FileName);
But when I run the application I didn't get an dialogbox, I only receive an empty messagebox


You didn't show the dialog box.

openFileDialog1.ShowDialog();


Was This Post Helpful? 0
  • +
  • -

#12 TomVdV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 28-May 09

Re: Relative path

Posted 01 June 2009 - 08:00 AM

View Postfixo, on 1 Jun, 2009 - 01:14 PM, said:

Add
using System.IO to reference scope

Then let you try:

   string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
				string strFileName = Path.Combine(execPath, "Opstelling.pdf");


I get a error on Assembly
Was This Post Helpful? 0
  • +
  • -

#13 TomVdV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 28-May 09

Re: Relative path

Posted 01 June 2009 - 08:18 AM

View PostSixOfEleven, on 1 Jun, 2009 - 02:57 PM, said:

You didn't show the dialog box.
openFileDialog1.ShowDialog();



			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			openFileDialog1.InitialDirectory = "c:\\";
			openFileDialog1.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
			openFileDialog1.FilterIndex = 2;
			openFileDialog1.RestoreDirectory = true;
			openFileDialog1.ShowDialog();

			MessageBox.Show(openFileDialog1.FileName);

			Process.Start("AcroRd32.exe", @"openFileDialog1");


I've an error te load the file in acrobat that the file can't be found
Was This Post Helpful? 0
  • +
  • -

#14 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Relative path

Posted 01 June 2009 - 07:40 PM

View PostTomVdV, on 1 Jun, 2009 - 09:18 AM, said:

View PostSixOfEleven, on 1 Jun, 2009 - 02:57 PM, said:

You didn't show the dialog box.
openFileDialog1.ShowDialog();



			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			openFileDialog1.InitialDirectory = "c:\\";
			openFileDialog1.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
			openFileDialog1.FilterIndex = 2;
			openFileDialog1.RestoreDirectory = true;
			openFileDialog1.ShowDialog();

			MessageBox.Show(openFileDialog1.FileName);

			Process.Start("AcroRd32.exe", @"openFileDialog1");


I've an error te load the file in acrobat that the file can't be found


You need to change this line:

Process.Start("AcroRd32.exe", @"openFileDialog1");



What you are passing Process.Start is: openFileDialog1 not the filename. When you put the quotes around it you are making what inside the quotes a string. You want to pass the variable, not the string.

You would want to do this to call the appropriate variable, like you did in the Message Box.

Process.Start("AcroRd32.exe", openFileDialog1.FileName);



Give that a try, hopefully it will help. :)
Was This Post Helpful? 0
  • +
  • -

#15 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Relative path

Posted 01 June 2009 - 10:32 PM

on btnClick Event use this code:
OpenFileDialog obj = new OpenFileDialog();
			if (obj.ShowDialog() == DialogResult.OK)
			{
				String _Path = obj.FileName;
				System.Diagnostics.Process.Start("Acrobat.exe", @_Path);
			}

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2