我正在尝试将值写入C#中的文件。我正在尝试使用以下代码:
public class Search : Controller
{
// GET: /Search/
public ActionResult List()
{
var model = new MyModel();
return View(model);
}
}
我收到错误: using (StreamWriter writer = new StreamWriter("values.txt"))
{
writer.Write("Word");
writer.WriteLine("word 2");
writer.WriteLine("Line");
}
有一些无效的参数。
或以下代码:
Error the best overloaded method match for System.IO.StranWriter.StreamWriter(System.IO.Stream)
我收到以下错误:string path = @"values.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
。我什么时候在这里失踪?
编辑:基本上我尝试将FIle更改为System.IO.File,我收到以下消息:The name 'File' does not exist in the current context
。我的代码在一个函数中,我从windows Application中的公共MainPage()函数调用。
EDIT2:我试图按照建议的重复帖子的解决方案。我收到了The type or namespace 'File' does not exist in the namespace System.IO(are you missing an assembly reference)
的消息。
答案 0 :(得分:3)
好的,我们必须在这里猜测很多,但看起来你正在编写手机,商店或通用应用程序。
新的Windows运行时没有为您提供所有旧的同步类和方法。
您的平台上没有FileStream(string)
构造函数且没有静态IO.File
类。
答案 1 :(得分:1)
我建议
IO.File.WriteAllText(fileFullPath, stringBuilder.ToString());
...而不是StreamWriter。
答案 2 :(得分:1)
我看到的是你试图在流对象中写, 您只需按照以下代码即可完成
System.IO.File.AppendAllText(@"{Filepath}","your text");
请注意,使用WriteAllText或,,,将用新值替换所有内容。
答案 3 :(得分:1)
您似乎正确使用using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
using (StreamWriter writer = new StreamWriter("test.txt"))
{
writer.Write("Line 1");
writer.WriteLine("Still line 1");
writer.WriteLine("Line 2");
}
}
}
}
。如果没什么用,请从头开始创建新的解决方案,控制台应用程序并粘贴此代码它必须工作:
test.txt
显然在bin文件夹中查找{{1}}文件