How to map network drive in windows service without using API in C#

G

Guest

hihi
I need to use windows service to connect network drive, so I cannot use "net use" to connect network drive. I hope it can connect the network drive under windows service. Therefore if I don't want to use API WNetAddConnection function, any C# Class can do it directly

thx...
 
V

Venkat Srinivasan.R

This posting is provided "AS IS" with no warranties, and confers no rights

Hi,

I found the following code hope it helps , site link is at the bottom :
================================================
.NET Framework Class Library :
ConnectionOptions Class
================================================
[C#]
-----------------------------START
CODE---------------------------------------------------------
public class ConnectionOptions : ManagementOptions

using System;
using System.Management;

// This example demonstrates how to connect to remote machine
// using supplied credentials.
class Sample_ConnectionOptions
{
public static int Main(string[] args) {
ConnectionOptions options = new ConnectionOptions();
options.Username = UserName; //could be in domain\user format
options.Password = SecurelyStoredPassword;
ManagementScope scope = new ManagementScope(
"\\\\servername\\root\\cimv2",
options);
try {
scope.Connect();
ManagementObject disk = new ManagementObject(
scope,
new ManagementPath("Win32_logicaldisk='c:'"),
null);
disk.Get();
}
catch (Exception e) {
Console.WriteLine("Failed to connect: " + e.Message);
}
return 0;
}
}
-------------------------- END CODE
--------------------------------------------------------------

URL :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemmanagementconnectionoptionsclasstopic.asp

Regards,
Venkat.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top