2

I'm looking everywhere in the internet but my code doesn't match the others. I have this

  string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fullname);

I can't have the Environment.SpecialFolder.Desktop to make it to Documents/Files How can I specify which folder my pdf document will be saved?

here is my full code.

string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fullname);
FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None);
Document doc = new Document(PageSize.A4, 60, 60, 40, 30);
PdfWriter w = PdfWriter.GetInstance(doc, fs);
doc.Open();
    .
    .
    .
doc.Close();
Fiendcoder1
  • 119
  • 1
  • 1
  • 12
  • Not sure I understand your problem, but you can just set the value of `outputFile` to where ever you want it go be and the file will be written there. What am I missing? – Adrian Sanguineti Dec 08 '16 at 23:56
  • Hey I tried what you said and I made this. `string outputFile ="C:\Users\Company\Documents\My Web Sites"` But it throws me an `UnauthorizedAccessException was unhandled`?? – Fiendcoder1 Dec 08 '16 at 23:58
  • That's because you didn't specify a file name in your `outputFile`, only the folder. – Adrian Sanguineti Dec 09 '16 at 00:01
  • There! I did it! Thanks man. I forgot to add the filename. Should you or me answer this? – Fiendcoder1 Dec 09 '16 at 00:05
  • I think you should see @GeniusBraiNs answer first. He has the better way than just hardcoding the filepath which is very limiting. Unless that is not what you were actually after. – Adrian Sanguineti Dec 09 '16 at 00:08

1 Answers1

2

If I understand you correctly, this is what you should be using:

string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), @"Documents/Files", fullname);

If your desired output folder isn't a subfolder of desktop, you can just remove the Environment.GetFolderPath(...) part and use whatever path you want. Don't forget to use escape the string (i.e. using "@"). And don't forget to combine the filename to the output folder path.

If it's not what you're after, then please provide more details.

  • But that's all the details there is to it. I tried what you suggest and it gives me `DirectoryNotFoundException` and it said that `Could not find a part of the path C:\Users\Company\Desktop\Documents\Files\fullname.pdf` – Fiendcoder1 Dec 09 '16 at 00:09
  • Please read this part of the answer: "If your desired output folder isn't a subfolder of desktop,..." – 41686d6564 stands w. Palestine Dec 09 '16 at 00:11
  • Oh okay yeah, sorry. I tried that and I ended up with this absolute path. `string outputFile = Path.Combine("C:/Users/Company/Downloads", fullname);` and it works. But what does the `@` is used for? – Fiendcoder1 Dec 09 '16 at 00:14
  • @Fiendcoder1, `DirectoryNotFoundException` means you have to create the directory first. You can do that by code – Adrian Sanguineti Dec 09 '16 at 00:15
  • `string outputFile = Path.Combine("C:/Users/Company/Downloads", fullname);` would also throw the `DirectoryNotFoundException` excepion if the folder did not exist either. Making your application create the folder itself will make it more robust. – Adrian Sanguineti Dec 09 '16 at 00:16
  • See http://stackoverflow.com/questions/9065598/if-a-folder-does-not-exist-create-it – Adrian Sanguineti Dec 09 '16 at 00:17
  • 1
    @Fiendcoder1, `@` is used to make a verbatim string instead of a regular string. check [this answer](http://stackoverflow.com/a/3312007/4934172) to know more. And you might also need to check [this MSDN article](https://msdn.microsoft.com/en-us/library/h21280bw.aspx) for what characters need to be escaped. – 41686d6564 stands w. Palestine Dec 09 '16 at 00:32