简体   繁体   中英

System.IO.File is a type not a namespace

Im tring to create a file using a System.Io.File namespace howeever im using it on MVC witch im new to and i get this error when i publish my proyect "A using namespace directive can only be applied to namespaces; 'System.IO.File' is a type not a namespace"

This is my using Statement:

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("]");
            }

the using keyword have different semantics depending on where it is located.

When put directly in a file it's to tell which namespaces to import. In that context you can not have a using statement for a class directly. Well. You can, but the syntax is different. MSDN .

The other usage is to dispose an object when it goes out of scope. In this case you can enter the fully qualified class name (namespace + class name) or just the class name. MSDN

In your code you have mixed the two.

Alternative 1

Completely remove the using statement in the file and just specify the full class name in the statement.

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"))

Alternative 2

Remove the namespace from the statement and the class name from the directive:

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"))

Alternative 3

Rename the class using a directive. You typically use this when the compiler can't distinguish between different identifiers (like having the same class name in different imported namespaces).

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"))

Your code is missing class and method declarations. System.IO.File is in fact a type and you shouldn't be referencing it in your using statements. All you need to reference is System.IO and then you can call 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");
            }   
        }
    }
}

MSDN on File.CreateText()

Try this.

 using (StreamWriter sw = File.CreateText(fileName))
    {
        sw.WriteLine("New file created: {0}", DateTime.Now.ToString());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM