Reputation: 59
I would make a simple program in C# with Windows forms, which gets some data given by the user thanks to some textboxes, and when He presses a button, a dialog
(I don't know which one) is displayed, in order to explore the pc folders and choose a destination for saving it there.
Well, I used a FolderBrowserDialog
(I don't know if that's the right one for the purpose), but there's a problem: in order to store a PDF with itext7, I have to give an Environment.SpecialFolder
variable, while the method selectedPath()
to get the user path of the formBrowserDialog returns a string.
I tried to convert the string
into Environment.SpecialFolder
in some way, but I always get a System.ArgumentException
Here's my code:
string name = txtName.Text;
//
//bla bla bla getting the parameters given by the user
//...
string pdfName = surname+ " - " + hours + "ː" + minutes + ".pdf";
string folder="";
//"fbd" is the FolderBrowserDialog
if (fbd.ShowDialog() == DialogResult.OK)
{
//here I get the folder path (I hope I've chosen the right dialog for this scope, which is a FolderBrowserDialog)
folder = fbd.SelectedPath;
//starting my pdf generation
//here is my attempt to write something in order to parse the path string into an Environment.SpecialFolder type, to use it as a parameter in getFolderPath()
Environment.SpecialFolder path = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), folder);
//here it's supposed to give to the GetFolderPath method the Environment.SpecialFolder type.
var exportFolder = Environment.GetFolderPath(path); //ON THIS LINE I GET THE EXCEPTION
var exportFile = System.IO.Path.Combine(exportFolder, pdfName);
using (var writer = new PdfWriter(exportFile))
{
using (var pdf = new PdfDocument(writer))
{
var doc = new Document(pdf);
doc.Add(new Paragraph("
//bla bla bla writing my things on it
"));
}
}
//pdf creation ends
}
Upvotes: 1
Views: 1105
Reputation: 344
To simplify all of this, you don't need to Environment.SpecialFolder
variable at all, nor do you need to pass it as a parameter.
The reason that an exception was thrown is because you tried to parse a string
into an Environment.SpecialFolder
variable enum
, when the string could not be parsed into one.
You can look here to see the list of enums included. I would wager that the specific path you selected matches none of those.
Here's what your code is currently doing:
enum
for a
special folderEnvironment.SpecialFolder
variable (so if you had actually been
able to parse it, you would've ended up with just what you started
with)You can simplify all of this by omitting steps 2 and 3, which cause the error.
string pdfName = surname+ " - " + hours + "ː" + minutes + ".pdf";
//You select the folder here
if (fbd.ShowDialog() == DialogResult.OK)
{
string folder = fbd.SelectedPath;
//combine the path of the folder with the pdf name
string exportFile = System.IO.Path.Combine(folder, pdfName);
using (var writer = new PdfWriter(exportFile))
{
using (var pdf = new PdfDocument(writer))
{
var doc = new Document(pdf);
doc.Add(new Paragraph("//bla bla bla writing my things on it"));
}
}
//Pdf creation ends
}
Upvotes: 1