[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

c# file text file access and reading in read mode.

Posted on 2013-02-04
3
Medium Priority
?
457 Views
Last Modified: 2013-02-10
Hi,

I have this line which puts all lines into an array 'lines[]'
lines = File.ReadAllLines(file);

Open in new window

However this is not ideal as it often throws an error when the file is being accessed by another application.

How can I use the fileaccess.Read option to solve this?

Thankyou
0
Comment
Question by:mhdi
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 668 total points
ID: 38853161
If the file is not opened in a mode that allows sharing between readers (and/or writers), then you won't be able to overcome this with code. The only thing you can do is wrap the logic in a try/catch and handle the exception accordingly.
0
 
LVL 10

Accepted Solution

by:
Monica P earned 668 total points
ID: 38853828
Try this way
http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

Read only mode :http://msdn.microsoft.com/en-us/library/y973b725.aspx

using System;
using System.IO;
class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file. 
        if (!File.Exists(path))
        {
            // Create a file to write to. 
            string[] createText = { "Hello", "And", "Welcome" };
            File.WriteAllLines(path, createText);
        }

        // This text is always added, making the file longer over time 
        // if it is not deleted. 
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);

        // Open the file to read from. 
        string[] readText = File.ReadAllLines(path);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}

Open in new window

0
 
LVL 15

Assisted Solution

by:Minh Võ Công
Minh Võ Công earned 664 total points
ID: 38858424
you can use Stream stream = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); to share read file.
//possible seclog paths
        String seclogPath1 = @"\\\\" + target + "\\C$\\Program Files (x86)\\Symantec\\Symantec Endpoint Protection\\seclog.log";
        String seclogPath2 = @"\\\\" + target + "\\C$\\Program Files\\Symantec\\Symantec Endpoint Protection\\seclog.log";

        //if seclog exists
        if (File.Exists(seclogPath1))
        {
            //output.AppendText("file exists at " + seclogPath1);
            //var seclogContent = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            Stream stream = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                //File.OpenRead(seclogPath1);
            StreamReader streamReader = new StreamReader(stream);
            string str = streamReader.ReadToEnd();
            output.AppendText(str);
            streamReader.Close();
            stream.Close();

        }

Open in new window

Check the topic:
http://stackoverflow.com/questions/12942717/read-log-file-being-used-by-another-process
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question