|
Use WMI[^].
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Hi All,
At the moment I'm converting the body of word and other documents to type Byte array using the following:
Byte[] b = Convert.FromBase64String(attach.Body);
This works fine for most word documents apart from those that have a body formatted using html tags. The result is that an exception is thrown "System.FormatException: Invalid character in a Base-64 string."
Does anyone know how I can convert the html formatted word document into a Byte array?
Thank you,
Mel
|
|
|
|
|
That is completely wrong. Do you have any idea what Base64 is? it only accepts 64 different characters and is intended for encoding binary data into text, making it ready for easy transmission or storage. You can't feed arbitrary text to Convert.FromBase64String and get away with it.
What you need depends on the context, which you didn't make clear. It may be simply reading an existing file as binary data (using File.Read or ReadAllBytes); or writing existing data to a MemoryStream, then calling MemoryStream.ToArray().
|
|
|
|
|
In that case maybe I assume you're saying I'm using the Base64 in the wrong way.
The only reason I'm using it is to convert the body of an attachment into a Byte array and then pass that byte array to a memory steam, which is then written to a FileStream.
Byte[] b = Convert.FromBase64String(attach.Body);
System.IO.MemoryStream mem = new System.IO.MemoryStream(b, false);
FileStream fsFile = File.OpenWrite(strTarPath + "\\" + strFName);
mem.WriteTo(fsFile);
MemoryStream takes in a Byte array and this is the reason for the convertion.
I looked for MemoryStream.ToArray() but it doesn't seem to exist.
Any ideas?
Thanks,
Mel
|
|
|
|
|
MWRivera wrote: I'm using the Base64 in the wrong way
No. You are not using it in the wrong way. It is wrong for you to use base64 at all.
Now explain what you need or want in functional terms, then people might be able to help you.
I will not provide any code, not even a class name, as long as you don't explain your goals.
|
|
|
|
|
Functional Terms
The code should take email attachments and save them to a directory, renaming the files in the process.
If anyone can provide help with this, I would really appreciate it.
Thank you,
Mel
|
|
|
|
|
well, you did not explain what an "email attachment" is; is it a file on disk to which you have a path? is it in memory? as a string? as a byte array?
Your OP had "attach.Body" in the code snippet but no declarations; a MailMessage has a Body property which is a string (BTW: a MailMessage should not be called attach IMO).
if the attachment is a string, get it and use File.WriteAllText; if it is a byte array, use File.WriteAllBytes.
PS: Please learn to help everyone, including yourself, by asking clear questions, providing all essential information.
|
|
|
|
|
Base64 is a number system - normal numbers like 1, 2, 3 are decimal or Base10, binary is Base2 etc.
Byte[] b = Convert.FromBase64String(attach.Body);
This would make sense IF "attach.Body" was in fact a Base64 string. Base64 still confuses me dude, so don't let Luc Pattyn make you feel bad for not being born with the intrinsic knowledge of Base64 and its use, as he must have been in order to provide him the right to speak to someone like that (I think a swing at his demeaning tone is fair enough, don't you ).
What type is the attach.Body property, is it a just a basic System.String? If you want to convert a string into a byte array, there are plenty of ways to do it, and it's something that comes up often (and I can NEVER remember how to do it). It also depends on the type of encoding you want.
In the System.Text namespace there are several Encoding classes: I usually use ASCIIEncoding:
string myText = "Hello world!";
ASCIIEncoding enc = new ASCIIEncoding();
byte[] dataBuffer = enc.GetBytes(myText);
There is also the UTF8Encoding class I can think of off the top of my head. The type of encoding you use does matter!
Having said all this, it seems like you're doing something odd. Can you explain to me what your actual intention is with "converting the html formatted word document into a Byte array"? What do you want to do that for? And then you said later that you wanted to save these files out to a directory. If I was you, I would do something like this:
using (var fileStream = new FileStream("theNewFileName.txt", FileMode.Create)
{
using (var streamWriter = new StreamWriter(fileStream)
{
streamWriter.Write(attach.Body);
}
}
Using the StreamWriter class, you can interact at a higher level and use strings rather than byte arrays - much clearer and simpler code IMHO.
Does this help?
|
|
|
|
|
NickHighIQ wrote: don't let Luc Pattyn make you feel bad for not being born with the intrinsic knowledge of Base64 and its use
Obviously I disagree.
1.
Yes, I happen to know for a long time what base64 means (something similar was used by Digital Equipment 30 years ago in their radix50 encoding they used to stuff three characters in a 16-bit word). However there is no need for such "intrinsic knowledge" to realize the approach is inappropriate. All it takes is reading up on the classes/methods used.
This is the very first line MSDN[^] offers about Convert.FromBase64String:
Converts the specified String, which encodes binary data as base 64 digits, to an equivalent 8-bit unsigned integer array.
I don't think of digits when a Word document has to be handled somehow. Not in base 10, not in hex, and not in a binary encoding of some kind.
When reading on, the same MSDN page also offers a good example, with encoding and decoding code, and encoded/decoded data. How it would fit the job at hand is a mystery to me, and I would hope to anyone who has looked at that page.
2.
I did not intend any demeaning tone, and frankly I don't see one. What I do here is providing information, advice, and guidelines. A universal guideline is this: read up on the class or method you are considering but are not familiar with.
|
|
|
|
|
Well, this is what I take objection to:
"That is completely wrong. Do you have any idea what Base64 is?"
And after a completely dignified and calm response (which I wouldn't have afforded you):
"No. You are not using it in the wrong way. It is wrong for you to use base64 at all."
I don't know about you, but where I'm from that's very belittling and seems like someone trying to assert to everyone just how clever and knowledgeable they are. Apologies if that was not your intention, but it seems very much that way to me.
Regardless, why not point him to the documentation? Not everything is "elementary knowledge", and that includes the labyrinth of MSDN. Also, Google is not very helpful if you don't know what you're after.
If you are so frustrated with people not providing enough information for you to correctly answer their question, better you not reply at all then make them feel stupid. I suggest you read the sticky post "HOW TO ANSWER A QUESTION" (although, given your knowledge of systems 30+ years old, I should think you would know how to answer a question):
"If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome."
I believe that includes sideline insults, like yours - while not directly calling him an idiot you might as well have.
|
|
|
|
|
And yet, here you are effectively calling Luc an idiot. Ironic, isn't it?
For your information, Luc is one of the most helpful people in this forum and certainly one of the most patient. Certainly, his patience to idiot threshold is far higher than mine. I suggest that you stop being quite so anal, and looking for offence where none was intended. Perhaps when you have made even 1/50th of the contribution to the site that Luc has, you might be better qualified to comment on his answering style.
|
|
|
|
|
I thank you for your advice, but the number of answers someone has posted on an arbitrary forum has no bearing on their ability to be helpful. The fact that you, also a prolific poster, called the OP an idiot makes me doubt even more the validity of the equation of contribution to helpfulness. All I'm saying is be helpful rather than treat him like a fool, something I've seen Luc do on more than one occasion, so I'm guessing it's not just that he's had a bad day.
I've had the joy of working for programmers of many years' experience who took delight, it seemed, in making me feel like an invalid when I didn't know, or was unaware of, some dark corner of the world of software engineering - I know how it feels. It's not a good feeling.
I will admit I did get a bit hot under the collar, and I apologise for this - I've just seen this so many times (especially considering my whopping 3 years of commercial experience) that I have trouble letting it lie.
|
|
|
|
|
|
Hi Nick,
I really appreciate your feedback. I have to agree with you, I have no doubt in thinking Luc is probably one of the most knowledgeable in these forums but from my experience he has been very unhelpful and I did take his feedback as being a little rude.
I refreshed on Base64 and found the following very helpful:
http://www.aardwulf.com/tutor/base64/base64.pdf
Here is an explanation of the process I’m trying to achieve…
I’m using a class library named SmtPop to allow the retrieval of email from a Pop3 server.
1. After the emails are retrieved from the server I check if they have attachments. attach is of type MimeAttachment
2. If an email has an attachment then the text within the attachment is accessed through attach.Body.
3. I then want to create a file to save the email attachment text into and give the file a different name to its original.
The only reason I’m converting the body of the attachment to a Byte array using Convert.FromBase64String, is because MemoryStream takes in a byte array as its first parameter in order to create a memory stream that can then be written to the file. FromBase64String is the only convert method that I know of that returns a Byte array.
Maybe there is an alternative way to the MemoryStream that doesn’t involve converting the attachment string to a Byte array?
Let me know if any of this isn’t clear.
Thanks again,
Mel
|
|
|
|
|
I'm going to read up on the StreamWriter now.
Thanks.
|
|
|
|
|
I found out that with MIMEAttachments, plain text word documents have "base64" as their ContentTransferEncoding value and HTML formatted word documents have "quoted-printable" as their ContentTransferEncoding value.
Does anyone know or can you refer me to a site that explains how to download quoted-printable formatted emails?
Thank you,
Mel
|
|
|
|
|
I have a generic event handler:
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
public class EventArgs_Generic<T> : EventArgs
{
private T _TargetObject;
public T TargetObject
{
get { return _TargetObject; }
}
public EventArgs_Generic(T Target)
{
_TargetObject = Target;
}
}
How do I list this in an interface? Something like...
event EventHandler<T> ....
Thanks
Everything makes sense in someone's mind
|
|
|
|
|
You can do this:
public interface IFoo<T>
{
event EventHandler<T> Bar;
}
Or you can just make the generic type concrete in the interface:
public interface IFoo
{
event EventHandler<int> Bar;
}
Depending upon your circumstances.
|
|
|
|
|
Say, I have
DataGridviewCheckBoxCell cell object. I can select/deselect it by doing
cell.Value = true /
cell.Value = false . This works until user manually selects/deselects some checkbox - after that this checkbox stops changing after assigning a value. What could be the reason for that?
|
|
|
|
|
Assuming you've bound the grid to a datasource, you have to modify the datasource, not the cell values.
|
|
|
|
|
In SQL Server I was doing this:
IF (record doesn't exists)
RETURN 999
and then I real the value using sqlParameter and Output Type is returnValue..
how can I do the same in mySQL
|
|
|
|
|
|
There's a "Database" forum further down the list of forums on the left.
|
|
|
|
|
Not a C# question, unless they've completely re-designed c# in the last few minutes.
|
|
|
|
|
Wow, you are here for more than 8 years, have already posted more than 500 messages and still haven't figured out, how to find the right forum for a question.
"I love deadlines. I like the whooshing sound they make as they fly by." (DNA)
|
|
|
|