I'm working on a Windows Service and I'm trying to write a Byte[]
to a already existing File with a normal FileStream. My Code looks like this:
byte[] attachBytes = attach.Data;
FileInfo info = new FileInfo(@"C:\Users\User\Documents\" + attach.Filename);
FileStream stream = info.Create();
stream.Write(attachBytes, 0, attachBytes.Length);
The exception message is:
Buffer cannot be null. Parameter name: array
Has anyone an idea what's going on here?
Additional info: byte[]
comes from a .msg
file which I'm reading with this library. I'm looping through the attachments of the .msg
file and saving them in a directory. .NET 4.5.2, Desktop Enviroment. I've tested the code in a console application and it works perfectly. But in my WindowsService project the error occurs.
EDIT:
Stacktrace:
Source: mscorlib Message: A call target caused an exception error. StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Source: mscorlib Message: Buffer cannot be null.
Parametername: array StackTrace:
at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
答案 0 :(得分:0)
以下是“公共重写无效Write(byte []数组,int偏移量,int计数)”方法的源代码:https://referencesource.microsoft.com/#mscorlib/system/io/filestream.cs行1751。在链接中为源代码授予了.NET版本。是4.7.2,我认为您的错误情况代码不会有所不同。
只有当传递给Write方法的'array'参数为null时,才从FileStream.Write(...)引发ArgumentNullException。
考虑“ attach.Data”可能确实返回null的可能性。关于“ attach.Data”,我们应该了解什么特别的东西?