Copy a file from the server to a local folder

Asked

Viewed 708 times

2

I am developing a routine in order to copy files from the server to a local folder, but unfortunately it does not copy. The idea is that every minute the files are copied.

I left one Sleep(5000) smaller to facilitate testing. There is no domain on the server and the folder on the server is mapped. You could take a look at?

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            ThreadStart start = new ThreadStart(CopiarArquivo);

            Thread thread = new Thread(start);

            thread.Start();
        }

        protected override void OnStop()
        {
        }

        public void CopiarArquivo()
        {

            for (int i = 0; i < 600; i++)
            {
                Thread.Sleep(5000);

            File.Copy(@"Z:\PARAM.SAC", @"C:\SACTRM\PARAM.SAC");
            }
        }
    }
}
  • What error is occurring ? It is possible that the user running the program is not allowed to access the mapped folder.

  • Hello Rafael. I made another routine makes an access to the folder on the server and displays the name of the file in Questa on the screen. It worked perfectly, so I’m not considering the possibility of permission.

  • Shows no error, simply does not copy.

3 answers

1

Apparently the problem is permission. Even if you don’t have a domain on the server, you can create a local user on each computer, but the name and password must be the same on both computers. After running the application with that user, windows interprets that it is the same user. Do not forget to give permission to read (File on server) and write (destination folder) to this user.

0

The same routine, only instead of a Windpws Service I used a Windows Form. It worked. Only I need it to be a service. I didn’t try your Rafael solution because I wouldn’t like to use users. But even with this test with WF you still think it is necessary to configure users ?. Thank you.

0

You can use the nomenclature UNC on the roads.

File.Copy(@"\\SVR\PARAM.SAC", @"C:\SACTRM\PARAM.SAC")

Also be aware of read permissions in the source file and write to the destination path.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.