我正在尝试使用System.Io.File名称空间创建一个文件,但是在MVC女巫Im上使用它,当我发布我的proyect时,我遇到了这个错误:“使用命名空间指令只能应用于命名空间;'System.IO.File‘是一种类型,而不是名称空间。”
这是我的使用声明:
using System;
using System.Reflection;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.IO.File;
using System.Text;
using (var reader = System.IO.File.CreateText(@"C:\inetpub\wwwroot\procedimiento.txt"))
{
// Starting outer json array
reader.WriteLine("[");
for (var rowIndex = 0; rowIndex < myTable.Rows.Count; rowIndex++)
{
var row = myTable.Rows[rowIndex];
var rowValues = new List<string>(); // can be reused if needed
foreach (DataColumn column in myTable.Columns)
rowValues.Add(row[column].ToString());
var jsonRow = JsonConvert.SerializeObject(rowValues);
// Write current row
reader.Write(jsonRow);
// Add separating comma
if (rowIndex != myTable.Rows.Count - 1)
reader.WriteLine(",");
}
// End outer json array
reader.WriteLine("]");
}发布于 2016-04-19 03:23:56
using关键字有不同的语义,这取决于它所处的位置。
当直接放入文件中时,就是要判断要导入哪些名称空间。在这种情况下,不能直接为类使用语句。井。你可以,但语法不同。MSDN。
另一种用法是在对象超出作用域时释放它。在这种情况下,您可以输入完全限定的类名(命名空间+类名)或只输入类名。MSDN
在您的代码中,您将两者混合在一起。
备选方案1
完全删除文件中的using语句,只需在语句中指定完整的类名。
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Text;
//in your method
using (var reader = System.IO.File.CreateText(@"C:\inetpub\wwwroot\procedimiento.txt"))备选方案2
从语句中删除名称空间,从指令中移除类名:
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
//in your method
using (var reader = File.CreateText(@"C:\inetpub\wwwroot\procedimiento.txt"))备选方案3
使用指令重命名类。当编译器无法区分不同的标识符(例如在不同导入的命名空间中具有相同的类名)时,通常使用此方法。
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using IoFile = System.IO.File; //this
using System.Text;
//in your method
using (var reader = IoFile.CreateText(@"C:\inetpub\wwwroot\procedimiento.txt"))发布于 2016-04-19 03:19:02
您的代码缺少类和方法声明。System.IO.File实际上是一种类型,您不应该在使用语句中引用它。您只需要引用System.IO,然后就可以调用File.CreateText()。
using System;
using System.IO;
public class MyClass
{
public void CreateFile()
{
string path = @"c:\temp\MyTest.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");
}
}
}
}发布于 2016-04-19 03:02:24
尝尝这个。
using (StreamWriter sw = File.CreateText(fileName))
{
sw.WriteLine("New file created: {0}", DateTime.Now.ToString());
}https://stackoverflow.com/questions/36707604
复制相似问题