Я не могу создать новый файл .txt с помощью класса StreamWriter - PullRequest
0 голосов
/ 29 июня 2018

Вот мой код ...

string path = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Music", "stream.txt");

StreamWriter sw = new StreamWriter("stream.txt");
sw.WriteLine("i am stream");
sw.Close();

Ответы [ 2 ]

0 голосов
/ 29 июня 2018

У вас там почти было решение:

string path = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"),"Music", "stream.txt");

            StreamWriter sw = new StreamWriter(path);
            sw.WriteLine("i am stream");
            sw.Close();

Вы просто должны были использовать созданную вами переменную пути:)

После выполнения не забудьте поискать в папке «Музыка» файл stream.txt

0 голосов
/ 29 июня 2018

Отметьте это: -

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        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");
            }   
        }

        // Open the file to read from.
        using (StreamReader sr = File.OpenText(path)) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(s);
            }
        }
    }
}

Ссылка

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...