In my previous post, I described how to connect to a network drive.
In this post, I will demonstrate how to disconnect from an existing connected network drive.
Suppose we have a drive Z: that is a network drive connected. Following is the code regarding how ti remove it.
Option Explicit
Dim WshNetwork, ShareName
Set WshNetwork = WScript.CreateObject("WScript.Network")
ShareName = "Z:"
WshNetwork.RemoveNetworkDrive ShareName, true, true
WScript.Quit
We are creating an object of WScript.Network and calling method RemoveNetworkDrive by passing the drive letter.
About Me
- Varun Sharma
- Hi.I am varun from kolkata. Engineering graduate and presently working in an IT company. Love to share my knowledge with all and to make lots of friends.
Categories
- .Net Framework 3.5 (5)
- ADO.NET (2)
- Asp.Net (9)
- C# (1)
- Common (1)
- Interview Experience (3)
- LINQ (6)
- OOPS (4)
- Sharepoint (33)
- UML (1)
- VBScript (4)
- Workflow Foundation (1)
Tuesday, September 8, 2009
How to disconnect a map drive using vbscript
Posted by
Varun Sharma
at
10:13 PM
0
comments
Labels: VBScript
How to map network drive using VBScript
Some time it is required to map a network drive using vbscript.
Today I will demonstate how to connet and then disconnect from a network drive.
In order to connect to a network drive, we need following data/information:
1. HomeServer Location: Actual path of the drive. In our example it is
"\\125.99.218.158\Data"
2. Name of the drive you wish to give: Let us say Z:
3. UserId/Password using which we will connect the map drive. If userid is same as logged in user then it is not required.
We will create an object of WScript.Network and call its method MapNetworkDrive
Here is the code:
Option Explicit
Dim strUser, strPassword, strDriveLetter, strHomeServer, strProfile
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
strDriveLetter = "Z:"
strHomeServer = "\\125.99.218.158\Data"
strProfile = "False" ' Mapping (not) stored in user Profile
strUser = "USERID"
strPassword = "PASSWORD"
objNetwork.MapNetworkDrive strDriveLetter,strHomeServer,strProfile,strUser,strPassword
WScript.Quit
So the above code is creating an object of Wscript.Network and calling method MapNetworkDrive by passwing parameters like drive letter, hom server, userid and password.
There is one more parameter that is strProfile. If set true, it will store the drive info in user profile and it will connect automatically next time when user logs in.
Posted by
Varun Sharma
at
10:04 PM
0
comments
Labels: VBScript
Wednesday, July 29, 2009
Tuesday, July 21, 2009
How to send mail using CDO in VBScript
Today we will see how to send mail using CDO in VBScript. The same code can be used in ASP, VB also with small modification.
STEP 1: Define the constants:
Const cdoSendUsingMethod="http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer ="http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort ="http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout ="http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic = 1
Const cdoSendUserName="http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword ="http://schemas.microsoft.com/cdo/configuration/sendpassword"
Const CdoReferenceTypeName = 1
STEP 2: Declare variables
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
Dim HtmlBody
STEP 3: Get a handle on the config object and it's fields
' Get a handle on the config object and it's fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
' Set config fields we care about
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = <SMTP-SERVER-NAME>
.Item(cdoSMTPServerPort) = <PORT-NUMBER>
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic
.Item(cdoSendUsingMethod ) = 2
.Update
End With
STEP 4: Create instance CDO.Messsage object
Set objMessage = CreateObject("CDO.Message")
STEP 5: Add image to the mail body
Add this step only if you have an image to add to the body
Set objBP = objMessage.AddRelatedBodyPart("C:\Users\Varun.Sharma\Shell\1.jpg", "1.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<1.jpg>"
objBP.Fields.Update
For description of step 5, visit: How to add image to mail body using CDO
STEP 6: assign the properties of CDO.Message object
Set objMessage.Configuration = objConfig
STEP 7: Build the HTML Body
Always prepare your html body before hand. That will help your code look better
HtmlBody="<img src=1.jpg><br>"
HtmlBody=HtmlBody & " Sending mail using CDO"
STEP 8: Set the CDO.Message mail properties
With objMessage
.To = To-Address
.Cc = CC-Address
.From = From-Address
.Subject = "SMTP Relay Test"
.HtmlBody=HtmlBody
.Send
End With
STEP 9: Release the CDO instances
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
FULL CODE:
Const cdoSendUsingMethod ="http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer ="http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort ="http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout ="http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate ="http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic = 1
Const cdoSendUserName ="http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword ="http://schemas.microsoft.com/cdo/configuration/sendpassword"
Const CdoReferenceTypeName = 1
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
Dim HtmlBody
' Get a handle on the config object and it's fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
' Set config fields we care about
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = SMTP-SERVER-NAME
.Item(cdoSMTPServerPort) = PORT-NUMBER
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic
.Item(cdoSendUsingMethod ) = 2
.Update
End With
Set objMessage = CreateObject("CDO.Message")
Set objBP = objMessage.AddRelatedBodyPart(PHYSICAL-PATH-OF-IMAGE, "1.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<1.jpg>"
objBP.Fields.Update
Set objMessage.Configuration = objConfig
HtmlBody="<img src=1.jpg><br>"
HtmlBody=HtmlBody & "Testing the mail"
With objMessage
.To = TO-ADDRESS
.Cc = CC-ADDRESS
.From = FROM-ADDRESS
.Subject = "SMTP Relay Test"
'.TextBody = "SMTP Relay Test Sent @ " & Now()
.HtmlBody=HtmlBody
.Send
End With
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
Posted by
Varun Sharma
at
5:42 AM
0
comments
Labels: VBScript
How to add image to mail body using CDO
Many times we face situation like, we need to send image in the html body of the mail that too using CDO.
Suppose we have an image at local server from where MailSend Code will get fire, here is the code:
Add this line of code to your CDO code to send image as a mail body:
Set objMessage = CreateObject("CDO.Message")
Set objBP = objMessage.AddRelatedBodyPart("C:\Users\Varun.Sharma\Shell\1.jpg", "1.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<1.jpg>"
objBP.Fields.Update
Here, in AddRelatedBodyPart,
argument 1 is : physical location of image
argument 2 is : identifier of the image (any name)
argument 3 is : To tell CDO to send the image as well
In next line, just put image identifer to the right hand side.
When sending mail refer the image in html body like this:
mail.HtmlBody="<img src=1.jpg" >
For full code of sending mail using CDO, check the following link:
How to send mail using cdo in vbscript
Posted by
Varun Sharma
at
5:35 AM
0
comments
Labels: VBScript
Sunday, July 5, 2009
Learning UML with C#
UML is a simple diagramming style that was developed from work done by Grady Booch, James Rumbaugh, and Ivar Jacobson, which resulted in a merging of ideas into a single specification and, eventually, a standard.
Here , We will see how to map a class and its relation in UML.
Basic UML diagrams consist of boxes representing classes. Let’s consider
the following class (which has very little actual function).
Basic Class :
public class Person {
private string name;
private int age;
//-----
public Person(string nm, int ag) {
name = nm;
age = ag;
}
public string makeJob() {
return "hired";
}
public int getAge() {
return age;
}
public void splitNames() {
}
}
We can represent this class in UML, as shown in Figure
The top part of the box contains the class name and package name (if any).
The second compartment lists the class’s variables, and the bottom compartment lists its methods. The symbols in front of the names indicate that member's visibility, where "+" means public, "-" means private, and "#" means protected. Static methods are shown underlined.
Abstract methods may be shown in italics or in an “{abstract}” label.
UML does not require that you show all of the attributes of a class, and it
is usual only to show the ones of interest to the discussion at hand.
Inheritance:
Now, we will look into inheritance and how to implement it in UML.
Let’s consider a version of Person that has public, protected, and private variables and methods, and an Emplo yee class derived from it.
public abstract class Person {
protected string name;
private int age;
//-----
public Person(string nm, int ag) {
name = nm;
age = ag;
}
public string makeJob() {
return "hired";
}
public int getAge() {
return age;
}
public void splitNames() {
}
public abstract string getJob(); //must override
}
We now derive the Employee class from it, and fill in some code for the getJob method.
public class Employee : Person {
public Employee(string nm, int ag):base(nm, ag){
}
public override string getJob() {
return "Worker";
}
}
You represent inheritance using a solid line and a hollow triangular arrow.
For the simple Employee class that is a subclass of Person, we represent this in UML, as shown in Figure
Note that the name of the Employee class is not in italics because it is now a concrete class and because it includes a concrete method for the formerly abstract getJob method.
Posted by
Varun Sharma
at
10:21 PM
0
comments
Labels: UML
Friday, June 19, 2009
OOPS Part II -- Pillars of OOPS -- Inheritance, Polymorphism, Encapsulation
Part I --> OOPS Part I -- OOPS Part I -- Understanding the Object Oriented Programming
All object-based languages must contend with three core principals of object-oriented programming, often called the "pillars of object-oriented programming (OOP)":
• Encapsulation: How does this language hide an object’s internal implementation details and preserve data integrity?
• Inheritance: How does this language promote code reuse?
• Polymorphism: How does this language let you treat related objects in a similar way?
The Role of Encapsulation
The first pillar of OOP is called encapsulation. This trait boils down to the language’s ability to hide unnecessary implementation details from the object user.
For example, assume you are using a class named DatabaseReader, which has two primary methods: Open() and Close():
// This type encapsulates the details of opening and closing a database.
DatabaseReader dbReader = new DatabaseReader();
dbReader.Open(@"C:\MyCars.mdf");
// Do something with data file and close the file.
dbReader.Close();
The fictitious DatabaseReader class encapsulates the inner details of locating, loading, manipulating, and closing the data file. Object users love encapsulation, as this pillar of OOP keeps programming tasks simpler. There is no need to worry about the numerous lines of code that are working behind the scenes to carry out the work of the DatabaseReader class. All you do is create an instance and send the appropriate messages
Closely related to the notion of encapsulating programming logic is the idea of data hiding. Ideally, an object’s state data should be specified using the private (or possibly protected) keyword. In this way, the outside world must ask politely in order to change or obtain the underlying value. This is a good thing, as publicly declared data points can easily become corrupted (hopefully by accident rather than intent!). You will formally examine this aspect of encapsulation in just a bit.
The Role of Inheritance
The next pillar of OOP, inheritance, boils down to the language’s ability to allow you to build new class definitions based on existing class definitions.
In essence, inheritance allows you to extend the behavior of a base (or parent) class by inheriting core functionality into the derived subclass (also called a child class).
Figure below shows a simple example. You can read the diagram in Figure as "A Hexagon is-a Shape that is-an Object."
When you have classes related by this form of inheritance, you establish "is-a" relationships between types. The "is-a" relationship is termed classical inheritance.
Here, you can assume that Shape defines some number of members that are common to all
descendents. Given that the Hexagon class extends Shape, it inherits the core functionality defined by Shape and Object, as well as defines additional hexagon-related details of its own (whatever those may be).
There is another form of code reuse in the world of OOP: the containment/delegation model
(also known as the “has-a” relationship or aggregation). This form of reuse is not used to establish parent/child relationships. Rather, the “has-a” relationship allows one class to define a member variable of another class and expose its functionality (if required) to the object user indirectly.
For example, assume you are again modeling an automobile. You might want to express the
idea that a car “has-a” radio. It would be illogical to attempt to derive the Car class from a Radio, or vice versa (a Car “is-a” Radio? I think not!). Rather, you have two independent classes working together, where the Car class creates and exposes the Radio’s functionality:
class Radio
{
public void Power(bool turnOn)
{
Console.WriteLine("Radio on: {0}", turnOn);
}
}
class Car
{
// Car 'has-a' Radio
private Radio myRadio = new Radio();
public void TurnOnRadio(bool onOff)
{
// Delegate call to inner object.
myRadio.Power(onOff);
}
}
Notice that the object user has no clue that the Car class is making use of an inner Radio object.
static void Main(string[] args)
{
// Call is forwarded to Radio internally.
Car viper = new Car();
viper.TurnOnRadio(false);
}
Difference Between Inherating a class and instantiating a class
When you inherit a class, you get the superclass attributes and methods that you can use without instantiating a superclass object. You can only use those which are public or protected.
If you instantiate them, you make an object and upon that object you call methods defined in the object's class.
When you inherit a class, base class from which a class is derived does not have state, behaviour or identity where as if you create an instance of a class, the base class sets it's identity, state, behaviour.
When you create an instance of an object, you can access the method of the class by using dot operator (.) but if you inherit a class, you can not access base class method by using dot operator.
Use Inheritance when you are sure that you need to extend few methods of the base class or need to add some more methods to it otherwise just create the instance of the class.
The Role of Polymorphism
The final pillar of OOP is polymorphism. This trait captures a language’s ability to treat related objects in a similar manner. Specifically, this tenant of an object-oriented language allows a base class to define a set of members (formally termed the polymorphic interface) that are available to all descendents. A class’s polymorphic interface is constructed using any number of virtual or abstract members.
In a nutshell, a virtual member is a member in a base class that defines a default implementation that may be changed (or more formally speaking, overridden) by a derived class. In contrast, an abstract method is a member in a base class that does not provide a default implementation, but does provide a signature. When a class derives from a base class defining an abstract method, it must be overridden by a derived type. In either case, when derived types override the members defined by a base class, they are essentially redefining how they respond to the same request.
To preview polymorphism, let’s provide some details behind the shapes hierarchy shown in
Figure. Assume that the Shape class has defined a virtual method named Draw() that takes no parameters. Given the fact that every shape needs to render itself in a unique manner, subclasses (such as Hexagon and Circle) are free to override this method to their own liking.
Once a polymorphic interface has been designed, you can begin to make various assumptions
in your code. For example, given that Hexagon and Circle derive from a common parent (Shape), an array of Shape types could contain anything deriving from this base class. Furthermore, given that Shape defines a polymorphic interface to all derived types (the Draw() method in this example), we can assume each member in the array has this functionality. Consider the following Main() method, which instructs an array of Shape-derived types to render themselves using the Draw() method:
class Program
{
static void Main(string[] args)
{
Shape[] myShapes = new Shape[3];
myShapes[0] = new Hexagon();
myShapes[1] = new Circle();
myShapes[2] = new Hexagon();
foreach (Shape s in myShapes)
{
s.Draw();
}Console.ReadLine();
}
}
This wraps up our brisk overview of the pillars of OOP. Now that you have the theory in your mind, the remainder of this chapter explores further details of how encapsulation is handled under C#. The next chapter will tackle the details of inheritance and polymorphism.
Posted by
Varun Sharma
at
1:56 AM
1 comments
Labels: OOPS
Articles
-
▼
2009
(69)
-
►
June
(12)
- OOPS Part II -- Pillars of OOPS -- Inheritance, Po...
- OOPS Part I -- Understanding the Object Oriented P...
- Interview Experience with PWC
- Structure of Sharepoint Site and Navigation
- How to get Data from Active Directory using C#
- New Features of dot Net 3.5 Framework
- Steps in Compliation in ASP.NET
- What's the difference between encapsulation and ab...
- What is an abstraction ?
- How to use RunWithElevatedPrivileges in Sharepoint...
- How to add a SPGroup to a Site Collection- Sharepo...
- How to get list of SPGroups in Sharepoint Site Col...
-
►
May
(24)
- Using Extension Method
- What is difference between dataset and datareader ...
- Overview of ADO.NET architecture
- Introduction to ASP.NET MVC
- LINQ Architecture and Components
- Introducing LINQ
- .Net Framework and VS Roadmap
- What is .Net Framework 3.5 ?
- Creating a WebService in ASP.NET , C#
- Difference between .Dwp and .Webpart
- Various properties of PeopleEditor:
- How to get all the values of PeopleEditor in Share...
- How to check if PeopleEditor is blank ?
- How to set current user in the PeopleEditor via Co...
- How to check whether a List is a Document Library?...
- How to Upload a File to a SharePoint Site from a L...
- How to delete a List from Sharepoint ?
- Difference between Update and SystemUpdate in Shar...
- Microsoft Office Sharepoint Authentication Explain...
- MOSS Object Model : Sharepoint
- How to cretae a new SPlist instance using the WSS ...
- what is Windows Workflow Foundation ?
- Foundation book on sharepoint 2007
- How to get distinct value from CAML query
-
►
April
(9)
- How to check whether SPList exists in Sharepoint?
- Using SPPropertybag to save value
- How to set People Picker value in Sharepoint
- How to get current login SPUSer in sharepoint
- How to open SPSite, SPWeb in ASP.NET
- ASP.NET from Scratch - Code Behind - Part III
- ASP.NET From Scratch - 1st Program - Part II
- ASP.NET From Scratch - Introduction - Part I
-
►
June
(12)