如何访问另一个进程正在使用的c#中的文本文件
How to access a text file in c# that is being used by another process
我有文本文件,modscan 正在使用该文件将数据写入文件。在特定时间我必须读取数据并保存在数据库中。在离线模式下,即;没有 modscan 使用它我可以读取数据并很好地保存在数据库中。然而,当它与 modscan 联机时,它给出了异常
Cannot access file as it been used by other process.
我的代码:
using System.IO;
string path = dt.Rows[i][11].ToString();
string[] lines = System.IO.File.ReadAllLines(@path);
路径有"E:\Metertxt.txt"
那么我需要做哪些更改才能在不干扰 modscan 的情况下阅读它。
我在谷歌上搜索了一下,发现这可能有用,但我不确定如何使用它
FileShare.ReadWrite
您可以使用 FileStream
打开已在另一个应用程序中打开的文件。如果你想逐行阅读它,你将需要一个 StreamReader
。这有效,假设文件编码为 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);
}
}
}
万一您真的需要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();
我有文本文件,modscan 正在使用该文件将数据写入文件。在特定时间我必须读取数据并保存在数据库中。在离线模式下,即;没有 modscan 使用它我可以读取数据并很好地保存在数据库中。然而,当它与 modscan 联机时,它给出了异常
Cannot access file as it been used by other process.
我的代码:
using System.IO;
string path = dt.Rows[i][11].ToString();
string[] lines = System.IO.File.ReadAllLines(@path);
路径有"E:\Metertxt.txt"
那么我需要做哪些更改才能在不干扰 modscan 的情况下阅读它。 我在谷歌上搜索了一下,发现这可能有用,但我不确定如何使用它
FileShare.ReadWrite
您可以使用 FileStream
打开已在另一个应用程序中打开的文件。如果你想逐行阅读它,你将需要一个 StreamReader
。这有效,假设文件编码为 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);
}
}
}
万一您真的需要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();