We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Just reading a file without modifying the same document

Thread ID:

Created:

Updated:

Platform:

Replies:

123566 Mar 31,2016 05:58 AM Apr 7,2016 02:47 AM ASP.NET MVC 4
loading
Tags: DocIO
Testname
Asked On March 31, 2016 05:58 AM

Imagine I read a word file and meanwhile my program tries to open that same file and throughs a Exception "the process is already in use", how can I do both working on a file and the c# program should open it just for reading ?

Suganya Rathinam [Syncfusion]
Replied On April 1, 2016 03:16 AM

Hi Customer,

Thank you for contacting Syncfusion support.

DocIO converts the input Word document to DocIO Object model document and closes the stream used for reading the input Word document. So the same document can be opened again for reading without any exception being thrown. Please find the following code snippets for your reference.


WordDocument document = new WordDocument();

//Open the template document

document.Open(@"..\..\Data\Template.docx");

//Open the same Word document for the second time

WordDocument doc = new WordDocument();

doc.Open(@"..\..\Data\Template.docx");


Note: When the Word document for reading is opened by any other application such as Microsoft Word, then the same document cannot be opened using DocIO since the document stream is opened in other application.

When using the FileStream for reading the Word document at sample level, the FileAccess mode should be Read. When it is specified as ReadWrite then the same document cannot be opened agained for reading or writing. Please find the following code snippets for your reference.


//Open the document in Read mode

FileStream file = new FileStream(@"..\..\Data\Template.docx", FileMode.Open, FileAccess.Read);

//Open the same document for the second time

FileStream file2 = new FileStream(@"..\..\Data\Template.docx", FileMode.Open, FileAccess.Read);


If we misunderstood any of your requirement then kindly elaborate the actual requirement with detailed description. Thereby we will analyse further on the mentioned case and will provided you appropriate solution.


Regards,
Suganya


Suganya Rathinam [Syncfusion]
Replied On April 1, 2016 03:32 AM

Hi Customer,

Kindly ignore our previous update.

DocIO converts the input Word document to DocIO Object model document and closes the stream used for reading the input Word document. So the same document can be opened again for reading without any exception being thrown. Please find the following code snippets for your reference.


WordDocument document = new WordDocument();

//Open the template document

document.Open(@"..\..\Data\Template.docx");

//Open the same Word document for the second time

WordDocument doc = new WordDocument();

doc.Open(@"..\..\Data\Template.docx");


When the Word document for reading is opened by any other application such as Microsoft Word, then the same document can be opened using DocIO in ReadOnly mode. Please find the following code snippets for you reference.


//Create a new Word document

WordDocument document = new WordDocument();

//Open the template document

document.OpenReadOnly(@"..\..\Data\Template.docx", FormatType.Docx);



When using the FileStream for reading the Word document at sample level, the FileAccess mode should be Read. When it is specified as ReadWrite then the same document cannot be opened agained for reading or writing. Please find the following code snippets for your reference.


//Open the document in Read mode

FileStream file = new FileStream(@"..\..\Data\Template.docx", FileMode.Open, FileAccess.Read);

//Open the same document for the second time

FileStream file2 = new FileStream(@"..\..\Data\Template.docx", FileMode.Open, FileAccess.Read);


If we misunderstood any of your requirement then kindly elaborate the actual requirement with detailed description. Thereby we will analyse further on the mentioned case and will provided you appropriate solution.


Regards,
Suganya


Testname
Replied On April 6, 2016 03:26 PM

iam using memorystream not filestream, how to do it than ?

Suganya Rathinam [Syncfusion]
Replied On April 7, 2016 02:47 AM

Hi,

If your requirement is to open the document in Memory Stream then you can use the GetManifestResourceStream System API to get the document from your project resources as Stream. This stream can then be converted to memory stream using the byte array. Please find the following code snippets for your reference.


/Get the resource in stream

Assembly execAssem = Assembly.GetExecutingAssembly();

WordDocument document = new WordDocument();

Stream input1 = execAssem.GetManifestResourceStream("Sample.Data.Template.docx");

byte[] streambytes1 = new byte[input1.Length];

input1.Read(streambytes1, 0, (int) input1.Length);

//Convert stream to memory stream

MemoryStream stream1 = new MemoryStream(streambytes1);

//Open the document form memory stream

WordDocument doc1 = new WordDocument(stream1, FormatType.Docx);

//Dispose the streams used

stream1.Dispose();

input1.Dispose();

//Load the document for the second time

Stream input2 = execAssem.GetManifestResourceStream("Sample.Data.Template.docx");

byte[]  streambytes2 = new byte[input2.Length];

input2.Read(streambytes2, 0, (int)input2.Length);

//Convert stream to memory stream

MemoryStream stream2 = new MemoryStream(streambytes2);

//Open the document form memory stream

WordDocument doc2 = new WordDocument(stream2, FormatType.Docx);

//Dispose the streams used

stream2.Dispose();

input2.Dispose();

//Save and close the document

doc2.Save(@"..\..\Data\Template_Stream2.docx", FormatType.Docx);

doc2.Close();

doc1.Save(@"..\..\Data\Template_Stream1.docx", FormatType.Docx);

doc1.Close();


Regards,
Suganya


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;