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

2 Answers 2

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, "");
Sign up to request clarification or add additional context in comments.

Comments

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();

1 Comment

Please explain what's wrong with the OP's code, and how is your code fixing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.