-1

EDITED: Apologies for the errors - I understand a wan't super clear.

I have created a class library which i want to fill with multiple methods/functions. I am working on one of those methods however i am struggling to use the method using a custom directory.

See Code of method in class library:

public class SharpFuntions
    {
        public static void CreateFile(string location){
            string path = location;
                try
                   {
                    // Delete the file if it exists.
                    if (File.Exists(path))
                        {
                    File.Delete(path);
                        }
                    // Create the file.
                    File.Create(path);
                        {
                        }
                    }
                catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                        }
        }           
    }

Now when i try and call this function and use a directory, it doesnt pass. See below:

static void Main(string[] args)
{  
    SharpFuntions.CreateFile(@"C:\User\Text.txt");
}

I'm not sure if the above is even possible. I just want to be able to call the function and possibly be able to insert a different Directory/filename each time I use it.

So the below works which i know, However I dont want to Hardcode the Directory/Filename

public class SharpFuntions
    {
        public static void CreateFile(){
            string path = @"c:\temp\MyTest2.txt";
                try
                   {
                    // Delete the file if it exists.
                    if (File.Exists(path))
                        {
                    File.Delete(path);
                        }
                    // Create the file.
                    File.Create(path)
                        {
                        }
                    }
                catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                        }
        }    
Zypherr
  • 53
  • 10
  • 2
    You can't seriously catch exceptions in a _library_ and dump them to console? – Uwe Keim Jun 11 '18 at 09:09
  • 2
    What do you mean by "it doesn't pass"? `"C:\User"` looks more like a directory than a file. Creating directories/files directly under `"C:\"` probably requires special permission. – Corak Jun 11 '18 at 09:11

2 Answers2

3

File.Create Method (String)

Creates or overwrites a file in the specified path.

path Type: System.String

The path and name of the file to create.

File.Delete Method (String)

Deletes the specified file.

File.Exists Method (String)

Determines whether the specified file exists.

SharpFuntions.CreateFile(@"C:\User");

...

// path is not a file name, it will never work

File.Create(path);

None of the methods you show use a path, they use a file name. You need to change that

A better example

public static void CreateFile(string fileName)
{
    try
    {
       // Delete the file if it exists.
       if (File.Exists(fileName))
       {
          // Note that no lock is put on the
          // file and the possibility exists
          // that another process could do
          // something with it between
          // the calls to Exists and Delete.
          File.Delete(fileName);
       }

       // create empty file
       using (File.Create(fileName));

       // Create the file.
       //using (FileStream fs = File.Create(fileName))
       //{
       //   Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
       //    Add some information to the file.
       //   fs.Write(info, 0, info.Length);
       //}

       // Open the stream and read it back.
       //using (StreamReader sr = File.OpenText(fileName))
       //{
       //   string s = "";
       //   while ((s = sr.ReadLine()) != null)
       //   {
       //      Console.WriteLine(s);
       //   }
       //}
    }   
    catch (Exception ex)
    {
        // log
        // message
        // output to a console, or something
        Console.WriteLine(ex.ToString());
    }
}

usage

string fileName = @"c:\temp\MyTest.txt";

CreateFile(fileName);

Update

I've updated the code to create an empty file, if that's what you want

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

you can try using FileInfo

 public static void CreateFile(string location)
            {
                var fileInfo = new FileInfo(location);
                try
                {
                    // Delete the file if it exists.
                    if (fileInfo.Exists)
                    {
                        fileInfo.Delete();
                    }
                    // Create the file and directory.
              if (!Directory.Exists(fileInfo.DirectoryName))
                    Directory.CreateDirectory(fileInfo.DirectoryName);


                    fileInfo.Create();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
XomRng
  • 171
  • 12