1

I have a method which will accept a list of byte arrays. My method needs to be able to create a .csv file for each of these arrays and then archive the files. I've mocked up something what I need to achieve in this small test method:

Directory.CreateDirectory("C:\\Users\\user\\Documents\\Temp\\Files\\TempDir");
File.Create("C:\\Users\\user\\Documents\\Temp\\Files\\TempDir\\Temp1.csv").Dispose();
File.Create("C:\\Users\\user\\Documents\\Temp\\Files\\TempDir\\Temp2.csv").Dispose();
File.Create("C:\\Users\\user\\Documents\\Temp\\Files\\TempDir\\Temp3.csv").Dispose();

ZipFile.CreateFromDirectory("C:\\Users\\user\\Documents\\Temp\\Files\\TempDir", "C:\\Users\\user\\Documents\\Temp\\Files\\ArchDir.zip");

The issue is that I'm writing to the disk with this method. The actual application this method will run on will be a service so I can't use filepaths for the method. Is it possible to create and archive files on the fly? The downloaded archive is going to be passed to a controller and returned to the client through a browser to give some context to this.

Novastorm
  • 1,439
  • 3
  • 26
  • 40
  • so, you want to create a CSV file in memory? Isn't that just a `string` or `StringBuilder`? How much data will be in the CSV file? – Jodrell Apr 04 '16 at 10:07
  • You could write the csv format to a `MemoryStream` - _https://msdn.microsoft.com/en-us/library/system.io.memorystream_ – Jeroen van Langen Apr 04 '16 at 10:08
  • maybe take a peek at Klaus answer for some hints: http://stackoverflow.com/questions/29907291/downloading-of-zip-file-through-asp-net-mvc-using-dotnetzip – DaFi4 Apr 04 '16 at 10:09
  • Thank you all! I was a bit lost how exactly to approach this. The MemoryStream approach is looking the best match for the requirements though it's always good to have fallbacks (when do things ever go according to plan?? ;) ) – Novastorm Apr 04 '16 at 10:13
  • 1
    A service can access the file system. It just likely won't have permissions to access user folders. If you call `Path.GetTempPath` it will give you a temporary place to read/write the file. – Jack Hughes Apr 04 '16 at 10:17
  • Jack Hughes is correct, if you run your service under the proper User Context, file access is possible. I like Novastorms intended approach tho, as It is more portable and easier to maintain if you avoid unnecessary file IO. – DaFi4 Apr 04 '16 at 11:22

0 Answers0