How to copy a file from the network to my computer using C#?

Asked

Viewed 832 times

9

I have a desktop application that needs to copy media files from a server, videos and images. The server is on the same network as the terminal on which the program will be installed. The problem is, that this terminal will not be logged in with user and password on the network, I will have to pass a user and a password for him to work with the files, but I did not find how to do this.

My implementation below operates perfectly on my PC, where I am logged in with network user and my password, it will fetch the files on the server and puts them in the way on my computer. But when I run it on the terminal that should be running the videos and images it says that the system is not allowed access.

if (System.IO.Directory.Exists(sourcePath))
            {
                string[] directoryes = System.IO.Directory.GetDirectories(sourcePath, "*", System.IO.SearchOption.AllDirectories);

                foreach (string directoriePath in directoryes)
                {
                    string[] files = System.IO.Directory.GetFiles(directoriePath);

                    if (files.Count() != 0)
                    {
                        // Copy the files and overwrite destination files if they already exist.
                        foreach (string s in files)
                        {
                            // Use static Path methods to extract only the file name from the path.
                            string fileName = System.IO.Path.GetFileName(s);
                            string destFile = System.IO.Path.Combine(targetPath, fileName);
                            System.IO.File.Copy(s, destFile, true);
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }

How can I first log in with user and password on the server and search for these files?

1 answer

5


  • Thanks, I’ll take a look and test tomorrow morning.

Browser other questions tagged

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