0

I am basically trying to trace a specific file on my PC and whenever the file's content changes, it should be uploaded to my FTP server. The problem I'm facing is that it won't let me upload to my FTP server while the file is currently being written on by another process. I have tried everything, including opening in Administrator etc but this error (550 Permission denied) comes from the FTP server.

Here is my code:

public static void Trace()
            {
                string checksum = "";
                while (true)
                {
                    if (CheckMd5(Path) != checksum)
                    {
                        UploadFile1();

                    }

                    checksum = CheckMd5(Path);
                    Thread.Sleep(5000);
                }
            }

            public static void UploadFile1()
            {
                var ftp1 = new myFTP();
                if (!File.Exists(Path))

                {

                }
                else
                {
                    var currentTime = CurrentTime; // gets the current time
                    ftp1.UploadFile(Path, timeRn);
                }
            }

 public void UploadFile(string filePath, string CurrentTime)
        {
            FtpWebRequest request =
                (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1/" + CurrentTime);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential("user", "password");
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;
            request.EnableSsl = false;


            FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            byte[] buffer = new byte[stream.Length];

            stream.Read(buffer, 0, buffer.Length);
            stream.Close();

            Stream reqStream = request.GetRequestStream();
            reqStream.Write(buffer, 0, buffer.Length);
            reqStream.Close();
        }
Leaves
  • 35
  • 1
  • 9
  • https://stackoverflow.com/questions/222089/in-c-if-2-processes-are-reading-and-writing-to-the-same-file-what-is-the-best is the code you need to integrate... Indeed you need cooperation from program that writes the data and hope for "append only" writes. – Alexei Levenkov Feb 29 '20 at 03:09

1 Answers1

0

You should probably restructure those 2 processes so that the file-changing process fires an event after it's done changing the file, while the ftp-uploading process should stop forcing its way in by looping and comparing checksum values (just let it sit dormant and wait for the file-done signal). That approach will improve perf of your app as a bonus (aside from the accuracy that you need).

Aside from that, maybe try using FileSystemWatcher class. You can filter for modification events only.

Hari Lubovac
  • 622
  • 4
  • 14
  • The problem is that 1 process is not part of my code. It's an external process that I can't control. Also, reading the file itself works fine. The problem is when I try to upload it via FileStream to the FTP server. I think it doesn't allow uploading of files while they're on write by another process, not sure though what exactly is causing this error because if I don't upload it to FTP my program works fine. – Leaves Feb 29 '20 at 18:48
  • can you wrap the uploadfile1 method in the try-catch, log the error (ex.ToString()), and paste the whole thing on the bottom of your question. – Hari Lubovac Feb 29 '20 at 18:52
  • I figured out the error now. It was because my method to get the current time was broken and would always upload the file with the same file name and the FTP server doesn't allow overwriting. – Leaves Mar 02 '20 at 15:56