1
Answer

File.Move() Issue with FileSystemWatcher

Administrator

Administrator

22y
7.5k
1
I am using the FileSystemWatcher method to watch a folder. When a file arrives it will move the file to a process folder. My problem is when a large file is put in the folder the move command errors with Unhandled Exception: System.IO.IOException: The process cannot access the file "c:\temp\test.zip" because it is being used by another process. Obviously the file has not finished copying to the directory. Is there an attribute on the file that I can check to see if it can be moved or is there a filter that I can use that will only trip the oncreated event after the file has completely copied to the folder.
Answers (1)
3
Jithu Thomas

Jithu Thomas

194 10.2k 108.1k Jul 24
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string inputFilePath = "input.txt";  // Path to the input file
        string outputFilePath = "output.txt"; // Path to the output file

        // Define column lengths
        int[] columnLengths = { 1, 21, 51, 81, 131, 141 };

        // Read all lines from the input file
        string[] lines = File.ReadAllLines(inputFilePath);

        // Create an array to hold the formatted lines
        string[] formattedLines = new string[lines.Length];

        // Process each line
        for (int i = 0; i < lines.Length; i++)
        {
            string line = lines[i];
            string formattedLine = "";

            // Split the line into columns based on the column lengths
            int currentIndex = 0;
            foreach (int length in columnLengths)
            {
                if (currentIndex < line.Length)
                {
                    int lengthToUse = Math.Min(length, line.Length - currentIndex);
                    string column = line.Substring(currentIndex, lengthToUse).Trim();
                    formattedLine += column.PadRight(length) + " ";
                    currentIndex += lengthToUse;
                }
            }

            // Add the formatted line to the array
            formattedLines[i] = formattedLine;
        }

        // Write the formatted lines to the output file
        File.WriteAllLines(outputFilePath, formattedLines);

        Console.WriteLine("File formatted successfully.");
    }
}

Dear 

Try this,

Accepted
3
Rajadurai

Rajadurai

1k 662 3.8k Jul 24

Hi Jithu,

 

Its Working thank you