相关疑难解决方法(0)

如果文件不存在则创建文件

我需要让我的代码读取文件是否存在创建else附加.现在它正在读取它是否确实存在创建和追加.这是代码:

if (File.Exists(path))
{
    using (StreamWriter sw = File.CreateText(path))
    {
Run Code Online (Sandbox Code Playgroud)

我会这样做吗?

if (! File.Exists(path))
{
    using (StreamWriter sw = File.CreateText(path))
    {
Run Code Online (Sandbox Code Playgroud)

编辑:

string path = txtFilePath.Text;

if (!File.Exists(path))
{
    using (StreamWriter sw = File.CreateText(path))
    {
        foreach (var line in employeeList.Items)
        {
            sw.WriteLine(((Employee)line).FirstName);
            sw.WriteLine(((Employee)line).LastName);
            sw.WriteLine(((Employee)line).JobTitle);
        }
    }
}
else
{
    StreamWriter sw = File.AppendText(path);

    foreach (var line in employeeList.Items)
    {
        sw.WriteLine(((Employee)line).FirstName);
        sw.WriteLine(((Employee)line).LastName);
        sw.WriteLine(((Employee)line).JobTitle);
    }
    sw.Close();
}
Run Code Online (Sandbox Code Playgroud)

}

c# streamwriter

67
推荐指数
5
解决办法
18万
查看次数

C#:前置文件的开头

使用C#将文本添加到文件开头的最佳方法是什么?

我找不到一种简单的方法来做到这一点,但想出了几个解决办法.

  1. 打开新文件,编写我想要添加的文本,将旧文件中的文本追加到新文件的末尾.

  2. 由于我要添加的文本应该少于200个字符,我以为我可以在文件的开头添加空格字符,然后用我想要添加的文本覆盖空白区域.

有没有其他人遇到过这个问题,如果有的话,你做了什么?

c# file-io

35
推荐指数
2
解决办法
4万
查看次数

为什么换行符不起作用?

我是C#文件处理的新手,我正在制作一个非常简单的程序.代码如下:

class MainClass 
{
    public static void Main()
    {
        var sw = new StreamWriter("C:\\Users\\Punit\\Desktop\\hello.txt");
        sw.Write("HelloWorld" +Environment.NewLine);
        sw.Write("ByeWorld");
        sw.Close(); 

        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码在文本文件中产生以下预期结果:

HelloWorld
ByeWorld
Run Code Online (Sandbox Code Playgroud)

我还写了一些像这样修改过的代码版本:

class MainClass 
{
    public static void Main()
    {
        var sw = new StreamWriter("C:\\Users\\Punit\\Desktop\\hello.txt");
        sw.Write("HelloWorld\n");
        sw.Write("ByeWorld");
        sw.Close(); 

        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

这里不使用

Environment.Newline
Run Code Online (Sandbox Code Playgroud)

我直接将"\n"添加到"HelloWorld"行.这产生了以下输出(在文本文件中):

HelloWorldByeWorld
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么第二段代码不起作用?(不在文本文件中生成换行符)

c# io

1
推荐指数
1
解决办法
4486
查看次数

"System.IO.IOException:进程无法访问文件'C:\ Test\test.txt',因为它正由另一个进程使用"

对C#来说很新.尝试迭代地写入.txt文件,我尝试使用它来实现解决方案:

如果不存在,则创建.txt文件,如果它确实附加了新行

我这样写了:

  var path = @"C:\Test\test.txt";

  try
  {
    if (!File.Exists(path))
    {
      File.Create(path);
      TextWriter tw = new StreamWriter(path);
      tw.WriteLine(message);
      tw.Close();
    }
    else if (File.Exists(path))
    {
      using (var tw = new StreamWriter(path, true))
      {
        tw.WriteLine(message);
        tw.Close();
      }
    }
  }
  catch (Exception e)
  {
    Console.WriteLine(e);
    throw;
  }
}
Run Code Online (Sandbox Code Playgroud)

无论文件是否存在,它都会生成相同的错误:

"System.IO.IOException: The process cannot access the file 'C:\Test\test.txt' because it is being used by another process"
Run Code Online (Sandbox Code Playgroud)

这次我做错了什么?

.net c#

1
推荐指数
1
解决办法
826
查看次数

标签 统计

c# ×4

.net ×1

file-io ×1

io ×1

streamwriter ×1