-2

I am trying to write values to file in C#. I am trying to use either the following code:

 using (StreamWriter writer = new StreamWriter("values.txt"))
 {
            writer.Write("Word");
            writer.WriteLine("word 2");
            writer.WriteLine("Line");
 }

I got the error: Error the best overloaded method match for System.IO.StranWriter.StreamWriter(System.IO.Stream) has some invalid arguments.

or the following code:

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

I got the following error: The name 'File' does not exist in the current context. When am I missing here?

EDIT: Basically I tried to change FIle to System.IO.File and I got the following message:The type or namespace 'File' does not exist in the namespace System.IO(are you missing an assembly reference). My code is in a function which I call from public MainPage() function in windows Application.

EDIT2: I tried to follow the solution of the proposed duplicate post. I got the message that await operator can only be used within an async method.

Jose Ramon
  • 5,572
  • 25
  • 76
  • 152

4 Answers4

3

OK, we have to guess a lot here but it looks like you're writing a Phone, Store or Universal App.

The new Windows Runtime does not provide you with all the old synchronous classes and methods.

There is no FileStream(string) constructor and no static IO.File class on your platform.

H H
  • 263,252
  • 30
  • 330
  • 514
  • I followed that instructions to create my project http://kinect.github.io/tutorial/lab01/index.html. It is a blank project for windows 8.1 – Jose Ramon Jun 25 '15 at 08:52
  • 1
    Yes, big difference between an "App" and an "Application" here. You can't use the 'normal' I/O code samples you will find, look especially for WinRT. – H H Jun 25 '15 at 08:54
1

I suggest

IO.File.WriteAllText(fileFullPath, stringBuilder.ToString());

...instead of the StreamWriter.

Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
1

What i see is you are trying to write in stream object, Simply you can do by following code

 System.IO.File.AppendAllText(@"{Filepath}","your text");

Please note using WriteAllText or ,,, will replace all the content with your new value.

Behzad
  • 857
  • 1
  • 8
  • 27
1

Looks like You're using StreamWriter correctly. If nothing helps, create new solution from scratch, console application and paste this code. It has to work:

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

Obviously look for you test.txt file in bin folder

voytek
  • 2,202
  • 3
  • 28
  • 44