简体   繁体   English

如何在另一个进程正在使用的C#中访问文本文件

[英]How to access a text file in c# that is being used by another process

I have text file which is being been used by modscan to write data into the file. 我有一个文本文件,modscan正在使用该文本文件将数据写入该文件。 At a particular time I have to read the data and save in database. 在特定时间,我必须读取数据并保存在数据库中。 In offline mode ie; 在离线模式下 without modscan using it I can read the data and very well save in database. 无需使用modscan,我就可以读取数据并很好地保存在数据库中。 however as it online with modscan it gives exception 但是,因为它与modscan在线存在异常

Cannot access file as it been used by other process. 无法访问文件,因为该文件已被其他进程使用。

My code: 我的代码:

using System.IO;
string path = dt.Rows[i][11].ToString();
string[] lines = System.IO.File.ReadAllLines(@path);

path has "E:\\Metertxt\\02.txt" 路径具有"E:\\Metertxt\\02.txt"

So what changes I need to make in order to read it without interfering with modscan. 因此,在不干扰modscan的情况下,需要进行哪些更改才能读取它。 I googled and I found this which might work, however I am not sure how to use it 我用谷歌搜索,发现它可能有效,但是我不确定如何使用它

FileShare.ReadWrite FileShare.ReadWrite

You can use a FileStream to open a file that is already open in another application. 您可以使用FileStream打开在另一个应用程序中已经打开的文件。 Then you'll need a StreamReader if you want to read it line by line. 然后,如果要逐行读取它,则需要一个StreamReader This works, assuming a file encoding of UTF8: 假设文件编码为UTF8,此方法有效:

using (var stream = new FileStream(@"c:\tmp\locked.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (var reader = new StreamReader(stream, Encoding.UTF8))
    {
        string line;

        while ((line = reader.ReadLine()) != null)
        {
            // Do something with line, e.g. add to a list or whatever.
            Console.WriteLine(line);
        }
    }
}

Alternative in case you really need a string[] : 如果您确实需要一个string[]

var lines = new List<string>();

using (var stream = new FileStream(@"c:\tmp\locked.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (var reader = new StreamReader(stream, Encoding.UTF8))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            lines.Add(line);
        }
    }
}

// Now you have a List<string>, which can be converted to a string[] if you really need one.
var stringArray = lines.ToArray();
FileStream fstream = new FileStream("@path", FileMode.Open,FileAccess.Read, FileShare.ReadWrite);
StreamReader sreader = new StreamReader(fstream);
List<string> lines = new List<string>();
string line;
while((line = sreader.ReadeLine()) != null)
    lines.Add(line);
//do something with the lines
//if you need all lines at once,
string allLines = sreader.ReadToEnd();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 进程无法访问另一个进程正在使用的文件C# - The process cannot access the file it is being used by another process c# c#无法访问文件,因为它正被另一个进程使用 - c# Cannot access file because it is being used by another process 文件正在由C#中的另一个进程使用 - File is being used by another process in c# C# “文件正在被另一个进程使用” - C# "File is being used by another process" 其他进程正在使用的C#文件 - C# File being used by another process 另一个进程正在使用C#文件 - C# file is being used by another process 该进程无法访问文件&#39;directory \\ file&#39;,因为它正被C#File Writer中的另一个进程使用 - The process can not access the file 'directory\file' because it is being used by another process in C# File Writer C# File.ReadAllText 进程无法访问该文件,因为它正被另一个进程使用 - C# File.ReadAllText The process cannot access the file because it is being used by another process IOException:该进程无法访问文件“文件路径”,因为它正在被C#控制台应用程序中的另一个进程使用 - IOException: The process cannot access the file 'file path' because it is being used by another process in Console Application in C# C#进程无法访问该文件,因为它正由另一个进程使用(文件对话框) - C# The process can't access the file because it is being used by another process (File Dialog)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM