Append & detach same files in c# - c#

I want to append 2 files and I want to detach the same files?
It's that possible in c#? I did some codes to append its possible but detach I don't know.
Any one know to do that please help .thanks,

To combine files into one and then extract files back you will need to define a file format and store information about file sizes, names, compression method, etc.
Combining exe- and txt-files into one won't work without extra info.
One easy way is to define separator, which will tell you when one file is finished and another one is started (possibly first few bytes after separator will be file name, then 0x00 and actual data).
Another way is to use header, where you keep all info. Header is usually consist of signature and then some data structures (file name, size) so that you can find file related data after reading just header.
Probably easiest for you to combine file would be to actually zip them. See and search for ZipAchieve (.net 4.5).

Related

how to validate a file is Proper CSV or not in .net core?

I have a APi which receives CSV files as IFormFile . I have to check if the sent file is a proper CS file or not. So i am doing below checks.
Checking the File Extension.
Checking the File content type.
Issue:- If any app will use the API, then it's feasible to change the file extension along with the content-type. So how to validate a proper CSV file? I didn't get any helpful article as of now.
e.g. a PDF file can be changed to a .CSV(in extension) file along with its content-type. But PDF file is not a valid CSV
NB:- Magic number is one of the process for .XLSX,.docx,.pdf etc.But for CSV its not applicable, tried the same & failed. Any other way to check it?
Closest you can get would be a robust TryParse method.
But instead of re-inventing the wheel, try first a few libraries, they might do the job:
https://github.com/TinyCsvParser/TinyCsvParser
https://github.com/nreco/csv
Note that CSV parsing can be a difficult task even though it's a simple format.
Even if you can't use a library, there are plenty of ideas you can grab from them.
If I were to detect CSV content, I'd do the following:
ensure that a line contains readable characters
optionally detecting file enconding might help
ensure that a line isn't incredibly long, else it's likely to be binary, see #1
detect that first line has repeating separators
try parse lines
More or less this:
find 1st index of CR/LF or LF
read up to that
find separators in it
try parse the rest of the file, check against column count
if it fails then it's probably not CSV
It's pretty much all the heuristics you can try unless I'm mistaken.

Pass byte array back to its original file from sql filestream

I have setup filestream on my mssql server, and it works pretty well thus far. Currently, I have one entity in my database, that I have added manually.
When I added my file, it was automatically converted to a byte[], which Similarly appears as a byte[] when I get it through my .NET Core application (surprise).
Optimally, I would like to know, how I can decode this byte array into the original file? I have read several places, that I need to provide the original extension of the file in order to do that.
However, I have not added such a column in my database - I could easily add such a column, but it seems odd to me, if it isn't possible to pass it back to its original format without providing additional parameters.
Therefore, is there a way, in which I can convert the byte array back to its original file so that the user easily can download it, without having to do some sort of comprehensive conversion?
I would happily like to know, if one of you can point me in a direction here.
A filestream column contains the file's contents, not its external metadata, like the original filename, extension, directory location and access control list. If you write the byte[] to disk with, eg, File.WriteAllBytes(string,byte[]) it will be a usable file.

C# My own file format

I'm looking to make my own file format .
that format should contains pictures/pdf/and other files ...
also need to know how I can packer-unpacker for this format to unpack files from it/pack in it & reading the pictures from my own format to picture boxes on my WinForm for example.
I've searched but didn't really found what I'am looking for
I hope someone can help me , thank you
Zip is an excellent choice. Because you can encrypt the file and of course reduce the file size in some cases (text and uncompressed things). But if you want to create your own file format you can easily decide rules for your storage and order inside the file. Then serialize the info into the file. For example by object serialization or by writing the binary date to file object by object .
if you really want to write your own file format then I would suggest one of two things. One, you could do it entirely in binary at which point you would want to do a 'chunk' format. Chunk format is to basically have a header to each subsection. The header contains the size of both the header as well as the size of the payload. Create a serialization class for your header then add the bytes to the filestream from your payload. Actually pretty easy to do.
Second (and easier) way to do this would be to create an XML format. Create a master class for your format then add all of the data as collections of sub classes under that. Once you have that, use any of .net xml serialization classes to serialize it out to disk.
You can also use SQLite for your purposes. It provides dbms power without needing server. That is popular solution for your problem.
System.Data.SQLite is an ADO.NET adapter for SQLite.

Save two TextRanges together with some strings into one file, and then load this later, whats the best/easiest approach?

I have two TextRanges from two different RichTextBoxes, and four strings from regular textboxes. I would like to save all this information in one file, and then be able to load it later. Whats the best approach?
I've been reading some about it, and it seems that reading all into one memorystream and then save it to a file is one way to do it. And then parse this content later.
Anyone that want to share some experience, and simple code?
For a simple approach consider creating a class with string properties for each of your textbox texts. You could then set the properties when you want to save your text, use XML serialization to save the class to an XML formatted file, and then read it back at a later time.
The advantage of this approach is that you will not need to hande low-level file handling or parsing yourself.
Searching for C# and XML Serialization will yield plenty of code examples.
One solution is already provided by you: save in file and read after.
Another could be, in case if the data is too big, is using http://msdn.microsoft.com/en-us/library/dd997372.aspx Memory Mapped Fies.

Options for header in raw byte file

I have a large raw data file (up to 1GB) which contains raw samples from a USB data logger.
I need to store extra information relating to the file (sample rate, description, trigger point, last seek position etc) and was looking into adding this as a some sort of header.
The header file should ideally be human readable and flexible so I've so far ruled out some sort of binary serialization into a header.
I also want to avoid two separate files as they could end up separated when copied or backed up. I remembered somebody telling me that newer *.*x Microsoft Office documents are actually a number of files in a zip. Is there a simple way to achieve this? Could I still keep the quick seek times to the raw file?
Update
I started using the binary serializer and found it to be a pain. I ended up using the xml serializer as I'm more comfortable using it.
I reserve some space at the start of the files for the xml. Simple
When you say you want to make the header human readable, this suggests opening the file in a text editor. Do you really want to do this considering the file size and (I'm assuming), the remainder of the file being non-human readable binary data? If it is, just write the text header data to the start of the binary file - it will be visible when the file is opened but, of course, the remainder of the file will look like garbage.
You could create an uncompressed ZIP archive, which may allow you to seek directly to the binary data. See this for information on creating a ZIP archive: http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx

Categories

Resources