首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.IO.File是类型而不是命名空间。

System.IO.File是类型而不是命名空间。
EN

Stack Overflow用户
提问于 2016-04-19 02:44:39
回答 3查看 894关注 0票数 0

我正在尝试使用System.Io.File名称空间创建一个文件,但是在MVC女巫Im上使用它,当我发布我的proyect时,我遇到了这个错误:“使用命名空间指令只能应用于命名空间;'System.IO.File‘是一种类型,而不是名称空间。”

这是我的使用声明:

代码语言:javascript
复制
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("]");
            }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-19 03:23:56

using关键字有不同的语义,这取决于它所处的位置。

当直接放入文件中时,就是要判断要导入哪些名称空间。在这种情况下,不能直接为类使用语句。井。你可以,但语法不同。MSDN

另一种用法是在对象超出作用域时释放它。在这种情况下,您可以输入完全限定的类名(命名空间+类名)或只输入类名。MSDN

在您的代码中,您将两者混合在一起。

备选方案1

完全删除文件中的using语句,只需在语句中指定完整的类名。

代码语言:javascript
复制
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

从语句中删除名称空间,从指令中移除类名:

代码语言:javascript
复制
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

使用指令重命名类。当编译器无法区分不同的标识符(例如在不同导入的命名空间中具有相同的类名)时,通常使用此方法。

代码语言:javascript
复制
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"))
票数 3
EN

Stack Overflow用户

发布于 2016-04-19 03:19:02

您的代码缺少类和方法声明。System.IO.File实际上是一种类型,您不应该在使用语句中引用它。您只需要引用System.IO,然后就可以调用File.CreateText()。

代码语言:javascript
复制
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");
            }   
        }
    }
}

File.CreateText()

票数 1
EN

Stack Overflow用户

发布于 2016-04-19 03:02:24

尝尝这个。

代码语言:javascript
复制
 using (StreamWriter sw = File.CreateText(fileName))
    {
        sw.WriteLine("New file created: {0}", DateTime.Now.ToString());
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36707604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档