Solved

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

Posted on 2013-02-04
3
441 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 167 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 167 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 166 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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