locked
C# code to download .CSV which is located in another machine in same network RRS feed

  • Question

  • Hello All,

    I have one requirement is I need to write the C# code to download the .CSV file which is located in another machine in same network.

    Please help me on how to proceed. Even any third party dlls are also fine.

    Best Regards,

    Hari


    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you Happy Programming! Hari

    Friday, May 8, 2020 5:38 AM

All replies

  • You're not downloading a file. All you're doing is copying a file from one computer to another computer as long as you present user credentials from the program running on your machine to access resources on another machine on the LAN.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-copy-delete-and-move-files-and-folders

    https://www.lifewire.com/network-file-sharing-in-microsoft-windows-818219

    Friday, May 8, 2020 5:56 AM
  • Hi Hariprasadbrk,

    Thank you for posting here.

    If you are copying a file from a shared folder that does not require authentication, you only need to write one line of code.

    File.Copy(@"\\another pc\Share\test.txt", @"d:\1.txt");

    If you are copying files from another computer that requires authentication, please refer to the following code.

        public  class ExtractData 
        {
            [DllImport("advapi32.DLL", SetLastError = true)]
            public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
            public void DoWork(string username,string domain,string password)
            {
                AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
                IntPtr token = default(IntPtr);
                
                if (LogonUser(username, domain, password, 2, 0, ref token) != 0)
                {
                    WindowsIdentity identity = new WindowsIdentity(token);
                    WindowsImpersonationContext context = identity.Impersonate();
                    try
                    {
                        File.Copy(@"\\another pc ip \d$\Data.txt", @"d:\1.txt");
                    }
                    finally
                    {
                        context.Undo();
                    }
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                ExtractData extractData = new ExtractData();
                extractData.DoWork("another pc username", "domain", "another pc password");
    
            }
        }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 8, 2020 8:06 AM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 12, 2020 8:34 AM