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. Image for the cookie policy date

Just reading a file without modifying the same document

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 ?

4 Replies

SR Suganya Rathinam Syncfusion Team April 1, 2016 07:16 AM UTC

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



SR Suganya Rathinam Syncfusion Team April 1, 2016 07:32 AM UTC

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



TE Testname April 6, 2016 07:26 PM UTC

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


SR Suganya Rathinam Syncfusion Team April 7, 2016 06:47 AM UTC

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


Loader.
Up arrow icon