简体   繁体   中英

C# - Program won't overwrite existing text file

My system creates a text file on the first button click of the program and appends various things to it on other button clicks. Ideally everything would be saved all at the end but as it is a monitoring system that uses a timer, some of the data does need to be written to file at specific times or it will be lost.

On the click of the first button (btnStart), the program locates/creates the main folder (given a name using a constant string) and locates/creates a sub folder (using text entered into a combination textboxes prior to the button click) this sets the directory for the text file as shown below in string (fileName):

    private void createDirectory()
    { //create output file in this folder using owner name and current date

        //main folder path (contains all files output from system)
        string rootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Horse Monitoring Records";
        //sub folder path (each patient has individual subfolder)
        string subDirectory = rootDirectory + "\\" + txtPatName.Text + "/" + txtOwnerName.Text;
        //file name (patient has file created for each operation)
        fileName = subDirectory +  "\\" + txtOwnerName.Text + "/(" + DateTime.Now.Date.ToString("dd-MM-yyyy") + ").txt";

        if (!Directory.Exists(rootDirectory)) //if main folder does not exist...
        {
            Directory.CreateDirectory(rootDirectory); //create it in My Documents
        }
        if (!Directory.Exists(subDirectory)) //if patient sub folder does not exist...
        {
            Directory.CreateDirectory(subDirectory); //create it in Patient-Owner format
        }
    }

The next function called writes the first bit of text to the file, as shown:

    private void saveFileDetails()
    {
        //Once case details have been entered, create new file using these details and add data input structure
        StreamWriter consoleFile = new StreamWriter(fileName, true);

        consoleFile.WriteLine("------------------------------------------- Case Details -------------------------------------------");
        consoleFile.WriteLine("\n\n");

        //patient and vet details
        ...
        }

Originally when I set this function up, I did not include the 'true' overload to the StreamWriter in the first line of the function. This led to a run-time error that told me the text file could not be opened as it was already opened by another process.

Since I added the 'true' overload, the system has worked fine, but I have just encountered a problem in that once a text file has been created, it cannot be overwritten as a text file normally would by a StreamWriter using the same path.

I do need to be able to overwrite files as they become obselete after a period of time and do need to be replaced, does anyone have any idea where I can go from here to fix this issue?

Thanks, Mark

StreamWriter's overload that takes a boolean is detailed here . true makes it append the content. If you always want to override the file, you can use File.OpenWrite; I believe you can also pass this to the constructor like the following:

new StreamWriter(File.OpenWrite(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))

See more about File.OpenWrite and FIleMode . File.OpenWrite can do the job by itself if you want to use that instead. But I believe since File.OpenWrite returns FIleStream, that can be passed to the StreamWriter constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM