I have a program that calls a stored procedure which works fine.
The result of this, gives me 10 files with the name of the different projects.
What I am trying to do is to be able to compress those 10 files and make a single .ZIP that contains these files and then I can delete all the 10 original .CSV files.
How could I do this?
Here is my code:
List<string> projectName = new List<string>();
List<string> category = new List<string>();
List<string> Subcategory = new List<string>();
int count = xml.GetElementsByTagName("projectName").Count;
for (int i = 0; i < count; ++i)
{
projectName.Add(xml.GetElementsByTagName("projectName")[i].InnerText);
category.Add(xml.GetElementsByTagName("category")[i].InnerText);
Subcategory.Add(xml.GetElementsByTagName("Subcategory")[i].InnerText);
}
for (int i = 0; i < count; ++i)
{
string outCsvFile = string.Format(@"C:\\temp\\{0}.csv", category[i] + " " + Subcategory[i]);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Connection = conn;
sqlCmd.CommandText = "testStoredProcedure";
sqlCmd.Parameters.AddWithValue("@ProjectTitle", projectName[i]);
sqlCmd.Parameters.AddWithValue("@Category", category[i]);
sqlCmd.Parameters.AddWithValue("@SubCategory", Subcategory[i]);
sqlCmd.Parameters.AddWithValue("@TaskType", "");
sqlCmd.Parameters.AddWithValue("@TaskStatus", "");
sqlCmd.Parameters.AddWithValue("@IsBaseLanguage", 1);
sqlCmd.Parameters.AddWithValue("@RaterDemographics", "FirtName,LastName,Email,Adress");
conn.Open();
SqlDataReader reader = sqlCmd.ExecuteReader();
using (StreamWriter file = new StreamWriter(outCsvFile))
{
List<string> items = new List<string>();
for (int j = 0; j <= 5; j++)
{
items.Add(reader.GetName(j));
}
file.WriteLine("\"" + string.Join("\",\"", items) + "\"");
while (reader.Read())
{
items = new List<string>();
for (int j = 0; j <= 5; j++)
{
items.Add(reader[j].ToString());
}
file.WriteLine("\"" + string.Join("\",\"", items) + "\"");
}
}
conn.Close();
}
After you have compressed the files you can add a loop to remove them:
To be able to compress you could use the classes included in .net
How to: Compress and extract files
how many with the class
ZipFile
to create the zipNow the issue is how to generate the zip without the files being blocked, for that you should surely use the
using
, this way when you finish building the zip when you exit using it is released to be able to delete the fileZip Files in C# 5.0 (.NET 4.5)
It is important to analyze how you use
there is the using that I comment so that when you finish assembling the zip the files are released, in this way you can delete them