vkandvia
vkandvia

Reputation: 107

Write a file to network location in Linux using C#

The below program written in C# which runs fine in Windows but when it comes to running in Linux ( inside a docker container) it doesn't translate the path properly.

  class Program
    {
        static void Main(string[] args)
        {
            try {
                 bool validLogin = ValidateUser("domain",  "username", "password" );
                    if (validLogin)
                    {
                        var path = "\\\\10.123.123.123\\folder$\\subfolder";
                        string fullPath = Path.Combine("\\\\10.123.123.123\\folder$\\subfolder", "file_name1");
                        string body = "Test File Contents";
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory((path));
                        }
                        File.WriteAllText(fullPath, body);   
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString() + ex.Message);
            }
        }

        public static bool ValidateUser(string domainName, string username, string password)
        {
            string userDn = $"{username}@{domainName}";
            try
            {
                using (var connection = new LdapConnection {SecureSocketLayer = false})
                {
                    connection.Connect(domainName, LdapConnection.DefaultPort);
                    connection.Bind(userDn, password);
                    if (connection.Bound)
                        return true;
                }
            }
            catch (LdapException )
            {
                // Log exception
            }
            return false;
        }
    }

What exact path should I use? I have tried all sorts of combinations.

Upvotes: 3

Views: 2930

Answers (2)

Tejas Parnerkar
Tejas Parnerkar

Reputation: 376

Few suggestions

  1. Check the hosing environment & manipulate paths accordingly.
  2. You can use Path.Combine() to formulate your paths.

An example of this application is below

using System.Runtime.InteropServices;

class Program
    {
        static void Main(string[] args)
        {
            try {
                 bool validLogin = ValidateUser("domain",  "username", "password" );
                    if (validLogin)
                    {
                        var path = Path.Combine("\\\\10.123.123.123", "folder$", "subfolder");
                        string fullPath = Path.Combine(path, "file_name1");

                        if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            path = path.Replace(@"\", @"/");
                            fullPath = fullPath.Replace(@"\", @"/");
                        }

                        string body = "Test File Contents";
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory((path));
                        }
                        File.WriteAllText(fullPath, body);   
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString() + ex.Message);
            }
        }

        public static bool ValidateUser(string domainName, string username, string password)
        {
            string userDn = $"{username}@{domainName}";
            try
            {
                using (var connection = new LdapConnection {SecureSocketLayer = false})
                {
                    connection.Connect(domainName, LdapConnection.DefaultPort);
                    connection.Bind(userDn, password);
                    if (connection.Bound)
                        return true;
                }
            }
            catch (LdapException )
            {
                // Log exception
            }
            return false;
        }
    }


Upvotes: 1

Octavio Galindo
Octavio Galindo

Reputation: 340

In windows, you have "Local Paths" which start with a letter that refers to a local drive, then you have networked paths, which begin with a double-backslash, followed by some domain/IP, followed by a directory share (all of which can be mapped to another letter, for ease of access)

To access network shares, from Linux, you need to Mount the share to some location of the Linux tree.

You can look at many examples on-line, here is one: Mounting and mapping shares between Windows and Linux with Samba

Also, the resulting Path will look nothing like what you have for Windows, so, you will need to know (somehow) that you are running under which SO, and configure your paths accordingly.

Upvotes: 2

Related Questions