我正在尝试写一个文本文件,我的条件是如果该文件中已经存在写入,那么它应该打印'另一行添加 - 消息'。所以我尝试了以下但是它抛出异常'它被另一个进程使用'并且我能理解我做错了什么:
public void WriteMessage(string message)
{
string path = @"C:\Users\AT\Documents\Visual Studio 2013\Projects\Sample_App\Sample_App\app_data\log.txt"; //File path
FileStream stream;
if (File.Exists(path))
{
stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); //This section used to read and write operations
using (StreamWriter writer = new StreamWriter(stream)) //Writing words to the text file
{
string[] lines = File.ReadAllLines(path); //Here is the exception - It's being used by another process
if (lines.Length > 0)
{
writer.WriteLine("\n" + "Another Line Added - " + message);
writer.Flush();
stream.Close();
}
}
}
else
{
/**If text file is created for the first time - Starts**/
stream = new FileStream(path, FileMode.Create); //This section used to read and write operations
using (StreamWriter writer = new StreamWriter(stream)) //Writing words to the text file
{
writer.WriteLine(message);
writer.Flush();
stream.Close();
}
/**If text file is created for the first time - Ends**/
}
}
现在为第一个文件创建的文件包含“ Hello World !! ”,当我向其添加另一个文本时,我希望得到以下输出:
预期输出:
Hello World !! ----仅限第一次使用
添加另一条线 - Hello World !!
添加另一条线 - Hello World !!
添加另一条线 - Hello World !!
我希望有一些想法或建议可以让它发挥作用,让我知道我做错了什么。这完全是一个简单的问题。我想在文件中再做一件事,如下所示:
输出:
1)Hello World !!
2)Hello World !!
注意:在上面的输出中,每当我运行控制台应用程序时,我都会递增数字。这意味着如果存在相同的写作,那么它应该增加1。我期待一个简单的Linq可以这样做,并且可以用一种简单的方式做到:(在这种情况下,我是否需要创建任何模型来克服文本文件?)
var query = (var c in fileName
select c);
foreach(var item in query)
{
//Do something here
}
我已经完成了以下工作但尚无法完成上述工作:
答案 0 :(得分:1)
File.ReadAllLines(path);
会创建另一个流。
将它放在FileStream上面应该修复它
由于您使用的是using
关键字,因此您无需关闭流
public static void WriteMessage(string message)
{
string path = @"log.txt"; //File path
FileStream stream;
if (File.Exists(path))
{
string[] lines = File.ReadAllLines(path);
stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); //This section used to read and write operations
using (StreamWriter writer = new StreamWriter(stream)) //Writing words to the text file
{
if (lines.Length > 0)
{
writer.WriteLine("\n" + "Another Line Added - " + message);
writer.Flush();
}
}
}
else
{
/**If text file is created for the first time - Starts**/
stream = new FileStream(path, FileMode.Create); //This section used to read and write operations
using (StreamWriter writer = new StreamWriter(stream)) //Writing words to the text file
{
writer.WriteLine(message);
writer.Flush();
}
/**If text file is created for the first time - Ends**/
}
}
答案 1 :(得分:1)
如果您及时运回原始流,则处理原始流是一种有效的文件处理方式。我们有更新的.NET Framework版本以及更简单和更好的文件处理功能:
public void WriteMessage(string message)
{
string path = @"C:\Users\AT\Documents\Visual Studio 2013\Projects\Sample_App\Sample_App\app_data\log.txt"; //File path
if (File.Exists(path))
{
File.AppendAllLines(path, new[]{ "Another line added - Hello world" });
}
else
{
File.AppendAllLines(path, new[]{ "Hello World!!" });
}
}
实际考虑文件内容的一个例子:
public void WriteMessageWithLineNumbers(string message)
{
string path = @"C:\Users\AT\Documents\Visual Studio 2013\Projects\Sample_App\Sample_App\app_data\log.txt"; //File path
if (File.Exists(path))
{
var lines = File.ReadAllLines(path);
var nextLine = string.Format("{0} - Hello World", lines.Length + 1)
File.AppendAllLines(path, new[]{ nextLine });
}
else
{
File.AppendAllLines(path, new[]{ "1 - Hello World!!" });
}
}
答案 2 :(得分:1)
StreamWriter
对于构造函数不需要Stream
参数。它可以直接使用文件本身的路径。通过这种方式,您可以省略FileStream
:
public static void WriteMessage(string message)
{
var path = @"../../sth.txt"; //File path
if (File.Exists(path))
{
string[] lines = File.ReadAllLines(path);//This section used to read and write operations
using (var writer = new StreamWriter(path, true)) //Writing words to the text file
{
if (lines.Length > 0)
{
writer.WriteLine("Another Line Added - " + message);
}
else
{
writer.WriteLine(message);
}
writer.Flush();
}
}
else
{
/**If text file is created for the first time - Starts**/
using (StreamWriter writer = new StreamWriter(path)) //Writing words to the text file
{
writer.WriteLine(message);
writer.Flush();
}
/**If text file is created for the first time - Ends**/
}
}