-2

I am trying to build a path to put a file there, but I am using string.Format and the / doesn't appears between the parameters. This is my example:

    string pdfFile = string.Format("{0}{1}{2}{3}", "MyPDF", "/", this.IdPDF, "/");

Can someone tell me why the / between the JPG and Id doesn't appear?

Here is the answer thanks Damith and rest of all!

string pdfFile = string.Format("{0}/{1}", "MyPDF", this.idPDF);
user2112420
  • 955
  • 5
  • 11
  • 26
  • -1: Please show data that reproduces the issue - it is unclear what "Id" is and what resulting value of "Folder" you see. As it stands now there is nothing wrong with the code (except trying to build path with String.Format instead of `Path.Combine`). Side note: "please" spelled differently (and totally unnecessary in the SO questions), the valid phrase for your spelling is "plz give me teh codez". – Alexei Levenkov Apr 22 '13 at 16:28
  • Please post the actual result you are getting, along with the value of `Id` being used. If I run your code, I get the correct output with the appropriate placement of '/'. – Jon Senchyna Apr 22 '13 at 16:30
  • @user2112420 how is the final path supposed to be? – Julián Urbano Apr 22 '13 at 16:35
  • I already did, the only variable is the This.IDPDF: I want to get this: /MyPDF/2/ and after that will be my file ex: /MyPDF/2/sales.pdf – user2112420 Apr 22 '13 at 16:36
  • @user2112420 then your code should work perfectly. Do you then get `/MyPDF/2/sales.pdf` instead of `/MyPDF2/sales.pdf`? See my edited answer below. – Julián Urbano Apr 22 '13 at 16:40
  • `string pdfFile = string.Format("/{0}/{1}/{2}", "MyPDF", this.idPDF, pdfName);` – Damith Apr 22 '13 at 16:40
  • 1
    What is the value of `This.IDPDF` and **what is the actual output that you get**? – Jon Senchyna Apr 22 '13 at 16:44
  • This.IDPDF is a value like 1, 2, 3..etc, it will be the name of the folder. I want to list in a folder PDF, folder with named ID(1,2..etc) depending of the user(imagine this.IDPDF is the ID of the user, so I will have all the PDF of the users) – user2112420 Apr 23 '13 at 06:56
  • The thing is that I dont get the slash /, if I do other way that I get the / but with another ID, I think is setting the ascii.. – user2112420 Apr 23 '13 at 07:15
  • Questions like this make one want to stop contributing to SO – Julián Urbano Apr 23 '13 at 12:05

1 Answers1

7

Use Path.Combine:

string folder = System.IO.Path.Combine(@"\MyPDF", Id, "sales.pdf");

This will generate something like \MyPDF\2\sales.pdf. In general, Path.Combine will concatenate all parameters to build a path. From the MSDN example:

string[] paths = {@"d:\archives", "2001", "media", "images"};
string fullPath = Path.Combine(paths);

fullPath would be d:\archives\2001\media\images.

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52