Click here to Skip to main content
15,888,297 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi!

The following XML file should be read via C#.

XML
<pclist>
  <pc>
    <drive>Z:\</drive>
    <server>C:\</server>
    <user>test</user>
    <password>12345</password>
  </pc>
  <pc>
    <drive>Z:\</drive>
    <server>C:\</server>
    <user>test2</user>
    <password>23456</password>
  </pc>
</pclist>

For each user (e.g. test, test2) the drive C:\ should be mapped on the drive Z:\ (which does not exist standardly).
I'm using Visual Studio 2010. WNetAddConnection2 is not working (why ever). Which 'using' do I have to code if I want to use WNetAddConnection2?
How else can i map the drives? And how can I read the XML file correctly and make it work properly?

Greetings,
Jo
Posted
Updated 22-May-15 1:33am
v3
Comments
virusstorm 22-May-15 13:46pm    
What you are asking for is no simple task.

Take a look at this link:
http://www.codeproject.com/Articles/90143/Mapping-Network-Drive-using-C

And this thread:
http://www.codeproject.com/Questions/524048/ReadplustheplusXMLplususingplusXElementplusinplusC

If you are running into specific issues, let us know and we can try to help.
YoshiZer0 1-Jun-15 5:55am    
Thank you very much for your help :)
With a few little modifications I was able to solve the problem.
gggustafson 22-May-15 22:37pm    
See http://www.pinvoke.net/search.aspx?search=WNetAddConnection2&namespace=[All]
Philippe Mori 1-Jun-15 12:43pm    
Windows has options to configure network password... Storing password like this is not very secur.
YoshiZer0 2-Jun-15 2:33am    
I have to admit it may not be the best way to safe passwords, but it was desired that way.

1 solution

Using the suggested code i was able to find a solution.

C#
public class DriveSettings
{
    private enum ResourceScope
    {
        RESOURCE_CONNECTED = 1,
        RESOURCE_GLOBALNET,
        RESOURCE_REMEMBERED,
        RESOURCE_RECENT,
        RESOURCE_CONTEXT
    }
    private enum ResourceType
    {
        RESOURCETYPE_ANY,
        RESOURCETYPE_DISK,
        RESOURCETYPE_PRINT,
        RESOURCETYPE_RESERVED
    }
    private enum ResourceUsage
    {
        RESOURCEUSAGE_CONNECTABLE = 0x00000001,
        RESOURCEUSAGE_CONTAINER = 0x00000002,
        RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
        RESOURCEUSAGE_SIBLING = 0x00000008,
        RESOURCEUSAGE_ATTACHED = 0x00000010
    }
    private 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
    }
    [StructLayout(LayoutKind.Sequential)]
    private struct NETRESOURCE
    {
        public ResourceScope oResourceScope;
        public ResourceType oResourceType;
        public ResourceDisplayType oDisplayType;
        public ResourceUsage oResourceUsage;
        public string sLocalName;
        public string sRemoteName;
        public string sComments;
        public string sProvider;
    }
    [DllImport("mpr.dll")]
    private static extern int WNetAddConnection2
        (ref NETRESOURCE oNetworkResource, string sPassword,
        string sUserName, int iFlags);

    [DllImport("mpr.dll")]
    private static extern int WNetCancelConnection2
        (string sLocalName, uint iFlags, int iForce);

    public static void MapNetworkDrive(string sDriveLetter, string sNetworkPath)
    {
        if (sNetworkPath.Substring(sNetworkPath.Length - 1, 1) == @"\")
        {
            sNetworkPath = sNetworkPath.Substring(0, sNetworkPath.Length - 1);
        }

        NETRESOURCE oNetworkResource = new NETRESOURCE();
        oNetworkResource.oResourceType = ResourceType.RESOURCETYPE_DISK;
        oNetworkResource.sLocalName = sDriveLetter + ":";
        oNetworkResource.sRemoteName = sNetworkPath;

        if (IsDriveMapped(sDriveLetter))
        {
            DisconnectNetworkDrive(sDriveLetter, true);
        }

        WNetAddConnection2(ref oNetworkResource, null, null, 0);
    }

    public static int DisconnectNetworkDrive(string sDriveLetter, bool bForceDisconnect)
    {
        if (bForceDisconnect)
        {
            return WNetCancelConnection2(sDriveLetter + ":", 0, 1);
        }
        else
        {
            return WNetCancelConnection2(sDriveLetter + ":", 0, 0);
        }
    }

    public static bool IsDriveMapped(string sDriveLetter)
    {
        string[] DriveList = Environment.GetLogicalDrives();
        for (int i = 0; i < DriveList.Length; i++)
        {
            if (sDriveLetter + ":\\" == DriveList[i].ToString())
            {
                return true;
            }
        }
        return false;
    }
}


C#
private void buMove_Click(object sender, EventArgs e)
    {
        bool success = true;
        if (teBoSource.Text == "")
        {
            MessageBox.Show("An obligatory field is not filled.");
        }
        else
        {
            int counter_move = 0;
            foreach (string path in pathlist)
            {
                sourcePath = pathlist[counter_move];
                fileName = System.IO.Path.GetFileName(sourcePath);

                XDocument source = XDocument.Load(@"C:\XMLFile1.xml");
                string ns = source.Root.Name.NamespaceName;
                XName itemName = XName.Get("PC", ns);

                foreach (XElement descendant in source.Descendants(itemName))
                {
                    driveLetter = descendant.Element(XName.Get("drive", ns)).Value;
                    destinationPath = descendant.Element(XName.Get("server", ns)).Value;

                    destinationFile = System.IO.Path.Combine(destinationPath, fileName);

                    if (!System.IO.Directory.Exists(destinationPath))
                    {
                        System.IO.Directory.CreateDirectory(destinationPath);
                    }

                    try
                    {                    
                        DriveSettings.MapNetworkDrive("Z", destinationPath);
                        System.IO.File.Copy(sourcePath, destinationFile, true);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Unable to copy the selected file(s).");
                        success = false;
                    }
                    DriveSettings.DisconnectNetworkDrive(driveLetter, true);
                }
                counter_move += 1;                   
            }
        }
        if (success == true)
        {
            MessageBox.Show("The file(s) was/were copied successful.");
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900