none
How to use Streamwriter to create a new file if same file name exist? RRS feed

  • Question

  • Hello all, I started to use Visual Studio recently. Currently I'm doing a project to record data then the system will create a text file contains all the recorded data. Before the data recording started, we have to input a name inside. So the problem I faced here is I tried changing the codes few times, the file keep overwrite if the same name is used. I want to separate it into different files, for example "username"(1).txt, "username"(2).txt. Can anyone teach me please? I'm new to C# also.

    Thanks in advance!

    Tuesday, April 9, 2019 9:48 AM

Answers

  • I would create a simple helper method

    using System.IO;
    
    namespace Utilities
    {
        class FileSystem
        {
            public static string GetNextFilePath(string directory, string filename, string extension)
            {
                string newPath = Path.Combine(directory, filename + extension);
                
                int i = 1;
                while(File.Exists(newPath))
                {
                    string uniqueId = string.Format("({0})", i);
                    newPath = Path.Combine(directory, filename + uniqueId + extension);
                    i++;
                }
    
                return newPath;
            }
        }
    }

    Then in your application, you would use it like this
    string myPath =    Utilities.FileSystem.GetNextFilePath(@"C:\Temp", "username", ".txt");
    System.IO.File.WriteAllText(myPath, "test 1");
    myPath = Utilities.FileSystem.GetNextFilePath(@"C:\Temp", "username", ".txt");
    System.IO.File.WriteAllText(myPath, "test 2");
    

    Tuesday, April 9, 2019 4:48 PM

All replies

  • I would create a simple helper method

    using System.IO;
    
    namespace Utilities
    {
        class FileSystem
        {
            public static string GetNextFilePath(string directory, string filename, string extension)
            {
                string newPath = Path.Combine(directory, filename + extension);
                
                int i = 1;
                while(File.Exists(newPath))
                {
                    string uniqueId = string.Format("({0})", i);
                    newPath = Path.Combine(directory, filename + uniqueId + extension);
                    i++;
                }
    
                return newPath;
            }
        }
    }

    Then in your application, you would use it like this
    string myPath =    Utilities.FileSystem.GetNextFilePath(@"C:\Temp", "username", ".txt");
    System.IO.File.WriteAllText(myPath, "test 1");
    myPath = Utilities.FileSystem.GetNextFilePath(@"C:\Temp", "username", ".txt");
    System.IO.File.WriteAllText(myPath, "test 2");
    

    Tuesday, April 9, 2019 4:48 PM
  • You don't specify if this is for Windows Forms. The answer might be different for WPF.

    Your fundamental problem is that you don't want to write over existing data, right? Based on your title, you are assuming that the solution is StreamWriter but you do not say StreamWriter in the question. So that makes it unclear what you really need to do.

    Probably StreamWriter is not the solution. Are you using the OpenFileDialog Class? That is probably the best solution. You can use the OpenFileDialog.CheckFileExists Property. You could also use the FileInfo Class after the OpenFileDialog to double-check if the file exists to not allow a file to be replaced.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, April 9, 2019 5:14 PM
  • thanks for the reply, Wild.Bill! I will try out the code!
    Thursday, April 11, 2019 2:59 AM
  • sorry, it was my first time using this forum. I was rushing with my project works.
    yup, i don't want to overwrite existing data, my supervisor suggested me to use streamwriter.

    So OpenFileDialog Class....I will look into it! Thanks for the help! Truly appreciate it!

    Thursday, April 11, 2019 3:01 AM
  • When asking for help, it is best to explain the problem to be solved as best as possible. Too often programmers try to solve the problem then they ask for help doing what they think they need to do. In this situation, the fundamental problem is that you need to not overwrite data. Your supervisor thinks that StreamWriter will help, but it might not be the best for this. Hopefully you now have enough to solve the problem but if not then it will help to have more about the problem you are trying to solve.


    Sam Hobbs
    SimpleSamples.Info

    Thursday, April 11, 2019 6:45 AM