I have the whole MS Word file itself saved into a byte array.A want to load it the way I would if it was on file system but with the minimal use of Microsoft.Office.Interop.Word because it is very slow when it gets the the .Open(args[])
part.
Asked
Active
Viewed 2.0k times
6

mathinvalidnik
- 1,566
- 10
- 35
- 57
-
What you want to do./.. You want to read the file .. or Write the file ??? – Aravind Aug 12 '13 at 10:28
-
I want to read its text content. – mathinvalidnik Aug 12 '13 at 10:29
-
I have answered .... check it ... – Aravind Aug 12 '13 at 10:43
-
Where am I supposed to check it ? – mathinvalidnik Aug 12 '13 at 11:51
-
See Down i have answered and put your file path in that code...you will get the answer – Aravind Aug 12 '13 at 12:10
-
Possible duplicate of [How to create Microsoft.Office.Interop.Word.Document object from byte array, without saving it to disk?](https://stackoverflow.com/questions/13227828/how-to-create-microsoft-office-interop-word-document-object-from-byte-array-wit) – Adam Jun 14 '17 at 08:57
3 Answers
4
Try this....
byte[] bte = File.ReadAllBytes("E:\\test.doc"); // Put the Reading file
File.WriteAllBytes(@"E:\\test1.doc", bte); // Same contents you will get in byte[] and that will be save here

Aravind
- 1,521
- 2
- 12
- 23
-
3It still uses filesystem I/O. I believe the author is looking for a solution that keeps the `byte[]` in memory without writing the file to disk? – Adam Jun 14 '17 at 08:34
2
There is no supported way to do it right off-the-bat using Interop.Word, as there are no methods supporting byte arrays.
As a viable workaround you can use a temporary file in the following way:
// byte[] fileBytes = getFileBytesFromDB();
var tmpFile = Path.GetTempFileName();
File.WriteAllBytes(tmpFile, fileBytes);
Application app = new word.Application();
Document doc = app.Documents.Open(filePath);
// .. do your stuff here ...
doc.Close();
app.Quit();
byte[] newFileBytes = File.ReadAllBytes(tmpFile);
File.Delete(tmpFile);
Fore additional info, read this post on my blog.

Darkseal
- 9,205
- 8
- 78
- 111
-
Can we Convert an Application object to byte[] array with saving a file ? – Mohan Singh Jan 16 '19 at 11:52
-2
The method public static byte[] ReadAllBytes(
string path
)
returns all the file information into a byte array. You dont have to worry about the stream, as the MSDN documentation says:
"Given a file path, this method opens the file, reads the contents of the file into a byte array, and then closes the file."
Check out this link if you want more information

Marcel James
- 834
- 11
- 20