Reputation: 185
I'm new to C# and would appreciate a little help.
I've a windows console application that emails logs and is scheduled to run every hour.
What I want is to zip those logs(after they have been emailed) in the same folder. ie. the folder the application is reading the logs from.
This is snipped of my code so far.
string[] files = Directory.GetFiles(@"C:\Users\*\Documents\target", "*.txt");
try
{
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile()) //i'm using dotnetzip lib
{
foreach (var file in files)
{
Console.WriteLine(file);
sendEMail(file);
zip.AddFile(file,"logs");
}
zip.Save("mailedFiles.zip");
}
}
What's happening with the above code is I'm able to create a zip file but not in the same folder where the application is reading from. Instead it creates the zipfile in my program's location(which makes sense).
How do I go about changing the location of the created zipfile. Also I want the individual logs to be replaced by the one zipfile that's created.
Upvotes: 0
Views: 177
Reputation: 2192
Try saving it by using the directory.
For example:
{
string directory = @C:\Users*\Documents\
zip.Save(directory + "mailedFiles.zip");
}
You should also use System.IO
to get the directories instead of hardcoding them.
Upvotes: 0
Reputation: 1515
You can save the zip file to any of the special folders available in the user folder. You can get paths for the special folders with the following line of code:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
If you want to save the zip file other than these special folders then there might be permission issues.
Upvotes: 3