How do I access a file share programmatically [duplicate]
Asked Answered
R

2

12

I have a windows forms app running on a machine that is not on a domain, that needs to be able to move a file from the local filesystem to a UNC path. I have a username and password for that path. I was wondering is there any way to do this directly with out execing the net.exe command?

Ideally I wouldn't have to map a drive.

Rhine answered 17/9, 2009 at 21:18 Comment(2)
I THINK you will need to map a drive. I haven't ound a way around it yet. However, see this post: https://mcmap.net/q/1008970/-asp-net-access-to-network-share/1435789#1435789 Someone else's answer was accepted, but I have code posted in another answer for simplifying a network drive.Gripping
Take look at this post. It uses P/Invoke to call Win32 API LogonUser() and friends. There is also some example code provided.Barren
D
4

The accepted answer on this question here seems like it would be worth looking into; it suggests using the Win32 API function WNetUseConnection.

From MSDN:

The WNetUseConnection function makes a connection to a network resource. The function can redirect a local device to a network resource.

Which seems to accomplish what you're looking for, with no mention of net.exe. Does this help?

Doldrums answered 17/9, 2009 at 21:25 Comment(1)
Thanks for the tip I'll take a look into it. I have to wonder why there isn't something similar already in .NET though.Rhine
A
14

You can use WNetAddConnection to accomplish this. You will have to pInvoke. the code below worked for me after I set up the pInvoke declarations. The second block of code (below) contains the pInvoke declarations -- just stick it inside of a class.


        public static void CopyFile(string from, string shareName, string username, string password)
        {
            NETRESOURCE nr = new NETRESOURCE();
            nr.dwType = ResourceType.RESOURCETYPE_DISK;
            nr.lpLocalName = null;
            nr.lpRemoteName = shareName;
            nr.lpProvider = null;

            int result = WNetAddConnection2(nr,  password,  username, 0);
            System.IO.File.Copy(from, System.IO.Path.Combine(shareName, System.IO.Path.GetFileName(from)));
        }


You will need to paste the following supporting code into a class (taken from pInvoke.Net). Make sure to add a using statment to your code:

using System.Runtime.InteropServices

        [DllImport("Mpr.dll", EntryPoint = "WNetAddConnection2", CallingConvention = CallingConvention.Winapi)]
        private static extern int WNetAddConnection2(NETRESOURCE lpNetResource, string lpPassword,  
                                      string lpUsername, System.UInt32 dwFlags);

        [StructLayout(LayoutKind.Sequential)]
        private class NETRESOURCE
        {
            public ResourceScope dwScope = 0;
            public ResourceType dwType = 0;
            public ResourceDisplayType dwDisplayType = 0;
            public ResourceUsage dwUsage = 0;
            public string lpLocalName = null;
            public string lpRemoteName = null;
            public string lpComment = null;
            public string lpProvider = null;
        };

        public enum ResourceScope
        {
            RESOURCE_CONNECTED = 1,
            RESOURCE_GLOBALNET,
            RESOURCE_REMEMBERED,
            RESOURCE_RECENT,
            RESOURCE_CONTEXT
        };

        public enum ResourceType
        {
            RESOURCETYPE_ANY,
            RESOURCETYPE_DISK,
            RESOURCETYPE_PRINT,
            RESOURCETYPE_RESERVED
        };

        public enum ResourceUsage
        {
            RESOURCEUSAGE_CONNECTABLE = 0x00000001,
            RESOURCEUSAGE_CONTAINER = 0x00000002,
            RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
            RESOURCEUSAGE_SIBLING = 0x00000008,
            RESOURCEUSAGE_ATTACHED = 0x00000010,
            RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
        };

        public enum ResourceDisplayType
        {
            RESOURCEDISPLAYTYPE_GENERIC,
            RESOURCEDISPLAYTYPE_DOMAIN,
            RESOURCEDISPLAYTYPE_SERVER,
            RESOURCEDISPLAYTYPE_SHARE,
            RESOURCEDISPLAYTYPE_FILE,
            RESOURCEDISPLAYTYPE_GROUP,
            RESOURCEDISPLAYTYPE_NETWORK,
            RESOURCEDISPLAYTYPE_ROOT,
            RESOURCEDISPLAYTYPE_SHAREADMIN,
            RESOURCEDISPLAYTYPE_DIRECTORY,
            RESOURCEDISPLAYTYPE_TREE,
            RESOURCEDISPLAYTYPE_NDSCONTAINER
        };

Artisan answered 17/9, 2009 at 23:24 Comment(0)
D
4

The accepted answer on this question here seems like it would be worth looking into; it suggests using the Win32 API function WNetUseConnection.

From MSDN:

The WNetUseConnection function makes a connection to a network resource. The function can redirect a local device to a network resource.

Which seems to accomplish what you're looking for, with no mention of net.exe. Does this help?

Doldrums answered 17/9, 2009 at 21:25 Comment(1)
Thanks for the tip I'll take a look into it. I have to wonder why there isn't something similar already in .NET though.Rhine

© 2022 - 2024 — McMap. All rights reserved.