Inside the directory of the .exe, there is a folder called exports
. I have a method that takes parameters for a directory and filename.
In this case, I want to give the directory as this exports
folder. The following is the current code:
public void saveFile(string dir, string filename)
{
ExcelPackage pck = new ExcelPackage();
// creating a spreadsheet...
// now save the file
System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(dir, filename) + ".xlsx");
pck.Save(file);
pck.Dispose();
}
// usage of the above method
saveFile(System.Reflection.Assembly.GetExecutingAssembly().Location + @"\exports", "myFileName");
This yields an exception: "The given path's format is not supported". How to solve this?
System.Reflection.Assembly.GetExecutingAssembly().Location
gives the full path to the file or exe, including the exe name
You cannot then tack a folder name on after it without first removing the exe name
Try using Path.GetDirectoryName
to remove the filename from the path:
var folder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
folder = Path.Combine(folder, "exports");
It is always a good idea to Directory.CreateDirectory(folder)
before you try to write a file to a folder path you've built. If the folder exists it's a non op, if it doesn't then it will create it. It can create multiple subfolders in one go
Bear in mind also that when installed properly in Program Files, an application doesn't automatically have the right to save files into a subfolder under Program Files. Your code here that saves data alongside the program will likely fail when the program is installed to Program Files