Friday, November 30, 2007
Monday, November 26, 2007
Creation of Directories in .NET
Creating a Directory
The Directory, DirectoryInfo, File and FileInfo classes in the .NET Base Class Library provide a simplified and powerful access to the files and directories in your System.
System.IO provides all the necessary classes, methods, and properties for manipulating Directory and files. In order to utilize the various methods within System.IO, users attempting to perform these operations must also have the required permissions to accomplish these types of actions; if they do not, a SecurityException will be thrown. Below you will find the main classes under this namespace.
Class Purpose/Use
1. BinaryReader and BinaryWriter - Read and write binary data.
2. Directory, File, DirectoryInfo, and FileInfo - Create, delete, and move files or directories. You can retrieve specific information about the files or folder by using of the properties defined in these classes.
3. FileStream - Access the files in a random order.
4. MemoryStream - Access data stored in memory.
5. StreamWriter and StreamReader - Read and write textual information.
6. StringReader and StringWriter - Read and write textual Information from a string buffer.
If you have ever run across a requirement to create a directory on the Server running your application, the System.IO namespace provides everything you need to accomplish such a task. Here I will show you two way to create a directory named MyDirectory on the C: Drive.
using System;
using System.IO;
namespace SystemIO
{
///
///
public class Yahiya
{
static void Main(string[] args)
{
CreateDirectory1();
}
public static void CreateDirectory1()
{
System.IO.Directory.CreateDirectory(@"C:\MyDirectory");
}
public static void CreateDirectory2()
{
string directoryPath = @"c:\MyDirectory";
System.IO.DirectoryInfo di = Directory.CreateDirectory(directoryPath);
}
}
}
The first method, CreateDirectory1, simply utilizes the CreateDirectory method that takes a string parameter. In my case I passed in @"C:\MyDirectory".
The second method, CreateDirectory2, performs exactly the same action as CreateDirectory1 with the only differences being I assigned @"C:\MyDirectory" to a string variable named directoryPath and then passed this variable into the CreateDirectory method.
Now that you are armed with the basics of creating a directory, we will now move into how to check if the directory in fact exist before we attempt to create it.
The Directory, DirectoryInfo, File and FileInfo classes in the .NET Base Class Library provide a simplified and powerful access to the files and directories in your System.
System.IO provides all the necessary classes, methods, and properties for manipulating Directory and files. In order to utilize the various methods within System.IO, users attempting to perform these operations must also have the required permissions to accomplish these types of actions; if they do not, a SecurityException will be thrown. Below you will find the main classes under this namespace.
Class Purpose/Use
1. BinaryReader and BinaryWriter - Read and write binary data.
2. Directory, File, DirectoryInfo, and FileInfo - Create, delete, and move files or directories. You can retrieve specific information about the files or folder by using of the properties defined in these classes.
3. FileStream - Access the files in a random order.
4. MemoryStream - Access data stored in memory.
5. StreamWriter and StreamReader - Read and write textual information.
6. StringReader and StringWriter - Read and write textual Information from a string buffer.
If you have ever run across a requirement to create a directory on the Server running your application, the System.IO namespace provides everything you need to accomplish such a task. Here I will show you two way to create a directory named MyDirectory on the C: Drive.
using System;
using System.IO;
namespace SystemIO
{
///
///
public class Yahiya
{
static void Main(string[] args)
{
CreateDirectory1();
}
public static void CreateDirectory1()
{
System.IO.Directory.CreateDirectory(@"C:\MyDirectory");
}
public static void CreateDirectory2()
{
string directoryPath = @"c:\MyDirectory";
System.IO.DirectoryInfo di = Directory.CreateDirectory(directoryPath);
}
}
}
The first method, CreateDirectory1, simply utilizes the CreateDirectory method that takes a string parameter. In my case I passed in @"C:\MyDirectory".
The second method, CreateDirectory2, performs exactly the same action as CreateDirectory1 with the only differences being I assigned @"C:\MyDirectory" to a string variable named directoryPath and then passed this variable into the CreateDirectory method.
Now that you are armed with the basics of creating a directory, we will now move into how to check if the directory in fact exist before we attempt to create it.
Sunday, November 25, 2007
Visual Studio 2008 Orcas changing the default browser
Every one has a different choice when it comes to browser. I personally like Firefox for surfing the net. I should say the number of addin’s available for Firefox are really very good and makes surfing the net and debugging (Javascript) very easy.
When we use Visual Studio and try to debug a Web Application, by default, it uses the IE. Many a times we do not want it use IE but some other browser (Like Firefox).
With Visual Studio 2008 this can be done very easily. To Change the default browser while using View in browser option just right click on the aspx and select “Browse with” context menu. In the next dialogue we can select or also add the browser there. We can also set the default browser for the Visual Studio in there.
When we use Visual Studio and try to debug a Web Application, by default, it uses the IE. Many a times we do not want it use IE but some other browser (Like Firefox).
With Visual Studio 2008 this can be done very easily. To Change the default browser while using View in browser option just right click on the aspx and select “Browse with” context menu. In the next dialogue we can select or also add the browser there. We can also set the default browser for the Visual Studio in there.
Friday, November 23, 2007
How to enable session state in web services
Some days back I was writing a few web services. I have coded web services many times, but never required to use Session State before. This time I required to check a few session variables before returning the correct data from the web service. I tried using the HttpSession like in a normal web page but got an error. After a little search found the right way to use HttpSession in webmethods. The simple trick is to enable the Session State before using it directly. This can be done by using the following attribute
[WebMethod(EnableSession = true)] // C#
// VB .Net
Once you specify these lines over your web method you can use the session object naturally.
[WebMethod(EnableSession = true)] // C#
Once you specify these lines over your web method you can use the session object naturally.
Silent install of the .NET Framework
When I talked to ISV, they often just want to use the .NET Framework as an implementation detail of their applications.. they don't want their users to have to go to windows update or MSDN to download the framework, and they don't want to have some Microsoft setup UI popping up during the install experience of their application.
For this scenario, we have a little known feature of setup where you can install it the .NET Framework silently.. that is with no-UI popping up at all. This allows you to have full control of the experience...
For .NET FX 3.0: the magic command line is: Dotnetfx3.exe /q
Oh, and if you are still on .NET Fx 2.0,
the command is: Dotnetfx.exe /q:a /c:”install /q”
For this scenario, we have a little known feature of setup where you can install it the .NET Framework silently.. that is with no-UI popping up at all. This allows you to have full control of the experience...
For .NET FX 3.0: the magic command line is: Dotnetfx3.exe /q
Oh, and if you are still on .NET Fx 2.0,
the command is: Dotnetfx.exe /q:a /c:”install /q”
Enhancements in 3.5.NET Languages
While Visual Studio 2008, the several variations of LINQ, and the ADO.NET Entity Framework are getting a lot of attention in the coming .NET Framework 3.5, there are also several key language enhancements on the near horizon. Many of the language enhancements (which will be found in VB 9 and C# 3.0) are the foundation of these more prominent new technologies. This article is a primer for some of the key enhancements that will be introduced with the .NET Framework 3.5 and how they relate to each other.
There are several .NET language enhancements to be introduced with Visual Studio 2008 including implicitly typed variables, extension methods, anonymous types, object initializers, collection initializers and automatic properties. These language enhancements, along with features like generics, are critical to the use of some of the new features, such as LINQ with the ADO.NET Entity Framework. What can be confusing is that these features are often referred to in the same conversation as LINQ. Because of this relation by association, you may be led to believe that these features are part of LINQ. They are not; they are part of the .NET Framework 3.5 and the VB 9 and C# 3.0 languages. They are very valuable in their own rights as well as playing a huge role for LINQ.
There are several .NET language enhancements to be introduced with Visual Studio 2008 including implicitly typed variables, extension methods, anonymous types, object initializers, collection initializers and automatic properties. These language enhancements, along with features like generics, are critical to the use of some of the new features, such as LINQ with the ADO.NET Entity Framework. What can be confusing is that these features are often referred to in the same conversation as LINQ. Because of this relation by association, you may be led to believe that these features are part of LINQ. They are not; they are part of the .NET Framework 3.5 and the VB 9 and C# 3.0 languages. They are very valuable in their own rights as well as playing a huge role for LINQ.
Subscribe to:
Posts (Atom)