In my Windows forms project, I am trying to save a file generated into a folder called "Invoice". I am able to save to the desktop, but how can it be saved to a subfolder? I know this is very simple fix, but did some research but no luck with the solution.
PdfWriter writer = PdfWriter.GetInstance(doc,
new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + ord + ".pdf",
FileMode.Create));
You can add the name of the folder in the same way that you add the name of the file:
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Invoice\\" + ord + ".pdf", FileMode.Create));
// ^^^^^^^^^^^^
You can also use string.Format
to compose the path, like this:
var pathToPdf = string.Format(
"{0}\\{1}\\{2}.pdf"
, Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
, "Invoice"
, ord
);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pathToPdf, FileMode.Create));
将“ \\\\”替换为“ \\ Invoice \\” + ord +“ .pdf”
If the folder does not exist then you need to create the folder and then write
Directory.CreateDirectory Method (String)
Creates all directories and subdirectories as specified by path.
Example:
string fileName = @"C:\Users\SomeUser\My Documents\Foo\Bar\Baz\text1.txt";
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
// ...
}
once done you can write into the folder like this
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Invoice\\" + ord + ".pdf", FileMode.Create));
I don't like having everything in one line... this is what I'd do
string myFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "MyFolder");
string filePath = Path.Combine(myFolder, ord + ".pdf");
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Create));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.