File name does not exist in current context

I am starting to program in C #. I get the error The name 'File' does not exist in the current context .

The problem should be in the line var v = File.ReadLines("dictionary.txt");

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication7 { class Program { static void Main(string[] args) { if (args.Length > 0) { var v = File.ReadLines("dictionary.txt"); 
+8
c #
source share
7 answers

Just add this using statement to the top of the file

 using System.IO; 

The compiler only recognizes classes from namespaces that you have in the current context. You add a namespace to the context using using statements. You can also use the fully qualified type name, such as System.IO.File , to refer to the class.

If you are using Visual Studio, you can also place the cursor on the problem character ( File ) and press Shift + Alt + F10

+15
source share

For anyone using Visual Studio 2015, this error will still occur, even if it refers to System.IO. The problem is that, by default, the Visual Studio 2015 project will focus on both the dnx451 infrastructure and dnxcore50 infrastructure, and the System.IO assembly is apparently not available for dnxcore50.

If you look in the project.json file, you will see the "frames" section. A quick fix is ​​to comment on the "dnxcore50" entry so that you only aim at dnx451:

 "dnxcore50": { "dependencies": { "System.Console": "4.0.0-beta-22816", "System.Collections": "4.0.10-beta-22816", "System.Linq": "4.0.0-beta-22816", "System.Threading": "4.0.10-beta-22816", "Microsoft.CSharp": "4.0.0-beta-22816" } } 
+4
source share

Add using System.IO; to the section of your use.

Class

File is located in the System.IO namespace.

Alternatively (if this is the only number in your code in which you use any type from System.IO ), you can use the full name File , for example:

 var v = System.IO.File.ReadLines("dictionary.txt"); 

But in the event that you need to access objects from a certain namespace several times in your code, it is better to include this namespace in applications.

+2
source share

You need to enable System.IO add using System.IO next to other messages.

+1
source share

Add a using statement:

 using System.IO 
+1
source share

I am running .Net Core in vs2017 and I have a similar problem.

To solve this problem, you must change the target structure and install System.IO.FileSystem.

Using the following methods:

  • Right-click the project and select properties.
  • in the tab "Application" tab and "Target structure" will change to .NetStandard1.6

Then, to install System.IO.FileSystem, run the following command in the Package Manager console

 Install-Package System.IO.FileSystem -Version 4.3.0 
0
source share

1. First we need to create an object like this.

  OpenFileDialog ofd = new OpenFileDialog(); 

2. Then you need to add this top of your code

  using System.IO; 

3. Finally, you can change your code as follows

 OpenFileDialog.Title -----> ofd.Title OpenFileDialog.Filter -----> ofd.Filter 
0
source share

All Articles