0

I want to create a "text.txt" (or any .txt) file that can later be written in, edited, and renamed into a .json file. I've commented out some failed attempts to do this, and any methods would be equally helpful.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace FileGenTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            string space;
            int name;
            string fileName;

            name = 1;
            space = " ";
            fileName = "text.txt";

            string newPath = @"C:\Project Manifest\"+ name;

            bool directoryExists = Directory.Exists(newPath);

            if (directoryExists)
            {
                Console.WriteLine("Directory" + space + '"' + newPath + '"' + space + "Exists");
                //using (StreamWriter sw = File.CreateText(newPath));
                //File.Create(name);
                File.Create(@"myfilename.ext");
            }
            else
            {
                Console.WriteLine("Directory" + space + '"' + newPath + '"' + space + "Does NOT Exist");
                Console.WriteLine("Creating Directory" + space + '"' + newPath + '"' + space + "...");
                //Directory.CreateDirectory(newPath);
            }
            Console.ReadLine();
        }
    }
}
DioptricG
  • 11
  • 2
  • 1
    File.WriteAllText(path, text); – TaW Apr 26 '20 at 08:09
  • 1
    C# create text file: https://learn.microsoft.com/en-us/dotnet/api/system.io.file.createtext c# create json file: https://stackoverflow.com/questions/16921652/how-to-write-a-json-file-in-c – Luuk Apr 26 '20 at 08:10

2 Answers2

1

.Net has a fairly extensive API to deal with the file system.
IMHO, the simplest way to create an empty textual file is using File.WriteAllText with an empty string as content:

// Of course, this path is just an example...
var fileFullName = @"c:/Users/DioptricG/Documents/MyEmptyTextFile.txt";

File.WriteAllText(fileFullName, "");
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
0

if your project dont successfully running try this code

        string space,fileName;
        space = " ";
        fileName = "text.txt";
        string newPath = @"your directory\" +fileName;
        if(Directory.Exists(newPath))
        {
            Console.WriteLine("Directory" + space + '"' + newPath + '"' + space + "Exists");
            File.CreateText(newPath+"\\"+fileName+".txt");
            Console.WriteLine("Succesfully Creating Text Document..!");
        }
        else
        {
            Console.WriteLine("Directory" + space + '"' + newPath + '"' + space + "Does NOT Exist");
            Console.WriteLine("Creating Directory" + space + '"' + newPath + '"' + space + "...");
            Directory.CreateDirectory(newPath);
            if(Directory.Exists(newPath))
            {
                Console.WriteLine("Succesfully creating directory..!");
            }
            else
            {
                Console.WriteLine("Don't creating directory..!");
            }
        }
        Console.ReadLine();