Best practice to store images in xml? [duplicate] - c#

This question already has answers here:
Convert Image to Bytes and save in Xml
(2 answers)
Closed 5 years ago.
I am writing a single exe file application that can load and save data files (txt, csv, xml and json), the user can choose a custom image, save it in a data file and send the file to another user who should then be able to open it and see the image.
Since I want the application to be user friendly and easy to use (I know that's repetition :) ) I want it to work with a single data file, instead of having user1 send all his images to user2 so that user2 can see them, so the way I see it - storing the image in the file is innevitable. Here comes the question what would be the best practice of storing the image?
I'm guessing a byte array is better than a string?

I would use a base64 encoded string, since it uses less space then hex encoding. Almost every programming language should have a library to convert from a stream or byte array to base64 and the other way around

Related

Optimized parse text file, to then upload to Excel [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
My project is to take large amounts of logs, output inside text files and parse some of the data to be made into Excel format.
There is a lot of garbage data in between not needed
This is how one portion of it is like:
2018-05-17 07:16:57.105>>>>>>
{"status":"success", "statusCode":"0", "statusDesc":"Message Processed Sucessfully", "messageNumber":"451", "payload":{"messageCode":"SORTRESPONSE","Id":"5L","Id":"28032","messageTimestamp":"2018-05-16 23:16:55"}}
I will first need to take the time stamp befor the "{}"
as it differs from the messageTimestamp
When generating the excel workbook
This is how it will look like in Excel:
------A-----------------------------------B--------------C
1. Overall time stamp ---------- status------- statusCode
2. 2018-05-17 07:16:57.105 - success --- -0
And so on...
payload has its own section of logs within its "{}"
so its section in excel will look like this:
F
1. payload
2. {"messageCode":"SORTRESPONSE","Id":"5L","Id":"28032","messageTimestamp":"2018-05-16 23:16:55"}
its content can be in one section that's not an issue.
A friend of mine have done something similar but it can take a few minutes to even generate even one relatively small excel document
My Question:
What is the most optimal way can I parse the data needed to then store it in an array or multidimensional array
to then push it into an excel document.
I would try to split the input text on newline characters, then parse the JSON part with Newtonsoft.Json.
I would highly advise to not try to parse the JSON yourself. The bottleneck here will be disk IO not in-memory processing, so make it easy to write correct code and use 3rd party libraries.
Once you have structured data representing the input, you can write each entry to an output file with only the fields you need.
For an Excel file, is CSV okay or do you need XLSX files? For CSV you can just write to a file directly, for XLSX I would recommend the EPPlus library.
https://www.newtonsoft.com/json
https://archive.codeplex.com/?p=epplus

C# write data into CSV file [duplicate]

This question already has answers here:
Should log file streams be opened/closed on each write or kept open during a desktop application's lifetime?
(13 answers)
Closed 6 years ago.
i need to write data into a CSV file each 600 msec using a c# application . The question: is better open and close file each time or keep it open until the end of write data actions? Note: i will change file name each day and each 60000 record
Thanck a lot for your opinions
CSV files are really easy to write to. If you don't know how to write to a file, the dotnetperls is your friend. You can simply call BinaryWriter.Write() to write anything. Write a value then a comma. That's it! If this file is going to be edited by the user at the time of running the application, then don't keep it open. Otherwise, keeping it open makes sure nothing unexpected happens.

Reading appended line from bottom of logfile [duplicate]

This question already has answers here:
Read last line of text file
(6 answers)
Closed 8 years ago.
Scenario is the following:
A (weather) service dumps sensor data into a log file/text file.
The new readings are appended to the bottom of a given (existing) file
New data is added at regular intervals (interval may or may not be known)
I need to parse the new information/line and send it off to another service.
I don't want to read the whole file every time, unless I have to.
EDIT: Sorry for the bad wording. "unless I have to" should be understood as if there is no other way around. I have seen the post/answer referenced and it seems a little extensive.
Framework is 4.5.x.
Thank you.
To get the the last line of a text file you can use this
File.ReadLines(myFileName).Last();
This is the simplest method, but is inefficient. You can write your own parser as show here

Converting file content to string and store in sharepoint column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a requirement to allow user to upload any file (pdf, doc, ect) to sharepoint and later on download the file as needed. the upload and download tasks are implemented as asp.net web app. Currently, I convert the file content (byte array) to base64 string and store in sharepoint. For download, I get the content from sharepoint and do convert from base64 to string then I write this string to browser. The result did not look like original file. What is wrong?
From your question it sounds like the problem is you are converting the Base64 data to a string. Since Base64 is a representation of binary data using only printable characters it works fine as a string, but interpreting binary data directly as a string can result in data corruption. It's like trying to edit an EXE file using Notepad.
You need to convert the Base64 back to a byte array and write that to the browser.

C# VS2008 how to hide string values when opening file.exe in notepad? [duplicate]

This question already has answers here:
How to hide a string in binary code?
(23 answers)
How can I securely embed a static string (key) in C#?
(6 answers)
Closed 9 years ago.
When a compiled .exe file is opened in notepad all of the string values are visible as plain text, there is a space between each letter but easy for anyone to read. This is the same not just for string objects but any object that has data inside quotes. My current method for securing this data is to put an encrypted string for the visible value and decrypt it upon use. However this still leaves the encrypted text visible in notepad along with its decryption key, leaving it a vulnerability to an attacker with persistence.
Is there a better way to hide this data? How can I make the .exe file have no, or very limited, readable text when opened in notepad?

Categories