How to send network credentials through c#

Viewed 33

The function below works only when I open the source and destination folder in File Explorer and enter network credentials manually. I did some research and I thought (NetworkCredential theNetworkCredential) will fix my issue however it didn't. I still have to enter the credentials manually. Any help is appreciated, thank you in advance!

networkcredentials

public bool TransferSingle(int id)
        {
            // Ahron single PC
            var ahron = _ahronRepository.GetAhronById(id);

            try
            {
                // Return isOK status if everything is ok
                bool isOk;

                // Get Ahron Credentials 
                var credentials = _ahronRepository.GetAhronCred(1);

                var username = Decrypt(credentials.User); 

                var pwd = Decrypt(credentials.Pwd);

                if (ahron != null)
                {
                    var status = Ping(ahron.Hostname);

                    var fullDestinationPath = ahron.DestinationPath + @"\" + ahron.Line;

                    if (status)
                    {
                        // If host is alive 
                        ChangeStatus(ahron.Id, true);

                        // Network Credentials
                        NetworkCredential theNetworkCredential = new NetworkCredential(username, pwd);
                        CredentialCache theNetCache = new CredentialCache();
                        theNetCache.Add(new Uri(@"\\" + ahron.Sourcepath), "Basic", theNetworkCredential);
                        theNetCache.Add(new Uri(@"\\" + ahron.DestinationPath), "Basic", theNetworkCredential);

                        if (!Directory.Exists(fullDestinationPath))
                        {
                            Directory.CreateDirectory(fullDestinationPath);
                        }

                        if (!Directory.Exists(ahron.Sourcepath.Trim()))
                        {
                            Directory.CreateDirectory(ahron.Sourcepath);
                        }

                        int numberOfTotalFiles = 0;
                        
                        foreach (var file in new DirectoryInfo(ahron.Sourcepath.Trim()).GetFiles())
                        {

                            var filePath = fullDestinationPath + file.Name;
                            if (File.Exists(filePath))
                            {
                                File.Delete(file.FullName);
                            }
                            else
                            {
                                file.MoveTo($@"{fullDestinationPath}\{file.Name}");
                                numberOfTotalFiles++;
                            }
                        }

                        // Total number of files
                        UpdateLastTransfer(ahron.Id, numberOfTotalFiles);


                        isOk = true;
                    }
                    else
                    {
                        isOk = false;
                        ChangeStatus(ahron.Id, false);

                        CreateAhronLog(ahron.Hostname, "Hostname was offline");
                    }
                }
                else
                {
                    isOk = false;
                }

                return isOk;
            } catch (Exception ex)
            {
                _logceService.Create("Critical", $"Ahron Credentials - {ex.Message}", DateTime.Now);

                if (ahron != null)
                {
                    CreateAhronLog(ahron.Hostname, ex.Message);
                }
                else
                {
                    CreateAhronLog("Anonymous", ex.Message);
                }

                return false;
            }
        }
0 Answers
Related