about C#, C++, Java, sql, java script etc.

Tuesday, April 13, 2010

Rename a printer using C-sharp (C#)

WMI provides an easy way to manage system resources such as printers, scanners, processes etc. In the example below, I show how to rename a printer using a WMI query in c#:
    private void RenamePrinter(string oldName, string newName)
{
ManagementObjectSearcher query = new ManagementObjectSearcher(
"SELECT * FROM Win32_Printer");

ManagementObjectCollection result = query.Get();

foreach (ManagementObject printer in result)
{
if (printer["name"].ToString() == oldName)
{
printer.InvokeMethod("RenamePrinter",
new object[] { newName });
return;
}
}
}

In a similar way, one can change a default printer:
    private void SetDefaultPrinter(string printerName)
{
ManagementObjectSearcher query = new ManagementObjectSearcher(
"SELECT * FROM Win32_Printer");

ManagementObjectCollection result = query.Get();

foreach (ManagementObject printer in result)
{
if (printer["name"].ToString() == printerName)
{
printer.InvokeMethod("SetDefaultPrinter",
new object[] {printerName});
return;
}
}
}
If you need some other useful WMI methods of the object Win32_Printer, check out WMI CIM Studio, a tool that comes with WMI Administrative Tools.

Tuesday, January 26, 2010

Writing/reading a protected file in asp.net

Some of you may run into the error "Access to the path '...' is denied" while creating a protected file in asp.net. Well, one should first check if the user account used to execute asp.code has enough rights to access a folder you try to save the file in. As long as one doesn't use impersonation this user is either NETWORK SERVICE or ASPNET.
I created a new user account for the asp.net impersonation, added necessary rights to the protected folder but was still receiving the same "Access denied" exception. Adding the permission "Log on as a batch job" for the user account solved the problem. It seems that Windows uses the batch-queue to perform I/O operations while accessing encrypted files. This only holds if files are accessed under asp.net. Win-Forms applications don't require this permission to access encrypted files.

*Steps* to solve the problem:
  1. Open Control Panel
  2. Administrative Tools -> Local Security Settings -> User Rights Assignments
  3. Find the policy Log on as a batch
  4. Add your user account/group into it

Sunday, January 24, 2010

Convert relative path to absolute in VB.NET

Recently I've found a good way to get an absolute path out of a base and relative paths. Before I used the method Directory.SetCurrentDirectory(). But changing the current directory to a base directory, then to one in a relative path and finally reading the resulting path with Directory.GetCurrentDirectory() is kind of clumsy.
.NET provides a better solution in the class Uri:

 ''' <summary>
 ''' Constructs and returns an absolute path if <paramref name="absoluteOrRelativePath"/> is a relative path.
 ''' Otherwise <paramref name="absoluteOrRelativePath"/> is returned.
 ''' </summary>
 ''' <param name="basePath"></param>
 ''' <param name="absoluteOrRelativePath"></param>
 ''' <returns></returns>
 Function GetAbsolutePath(ByVal basePath As String, ByVal absoluteOrRelativePath As String) As String
   Dim baseUri As Uri = New Uri(basePath)
   Dim resultUri As Uri = baseUri

   Uri.TryCreate(baseUri, absoluteOrRelativePath, resultUri)

   Return resultUri.AbsolutePath
 End Function 

Examples:
GetAbsolutePath("c:\temp\", "cache/") c:/temp/cache/
GetAbsolutePath("c:\windows\app.conf", "system32/") c:/windows/system32/
GetAbsolutePath("c:\windows\temp\", "d:\cache\") d:/cache/

Sunday, November 8, 2009

Installing dsniff on debian

I am the one who uses package managers to install/update software and believes that is actually a good way. There are some people who don't use them at all and relay on manual installation only using make, make install. Well, this approach works perfectly until software doesn't have a lot of dependencies which are difficult to carry out manually.. That happened to me when I tried manually to install dsniff under Debian. The command apt-cache search dsniff didn't find the required package and therefore I downloaded sources and tried to install them manually. No success, coz there were required many other libraries to be installed which on the other hand couldn't not be installed due to conflicts with already installed software..

Since I am a newbie to debian I didn't know that there are stable and unstable branches in the package manager and so didn't find dsniff under stable branch. Here is a way how to install dsniff:
  • first update your package list with unstable software:
  • apt-get -t unstable update
  • then install the package:
  • apt-get install dsniff

Thursday, October 15, 2009

Switching styles dynamically

Recently on the project I am working on we had a problem regarding changing stylesheets. There was a big tree and we needed to have as fast as possible way of changing styles of all nodes in the tree. One solution is to construct a jquery selector, iterate through all the nodes and change styles respectively. It is a nice approach at the first hand but it turns out to be an inefficient one. For this reason we decided to change global styles, thereby not spending running time on js loops.

I measured performance for different approaches. Below are results.
First I generated a page with 10 thousand div tags on it:
<html>
<head>
  <script type="text/javascript" src="scripts/jquery.js"></script>
  <script type="text/javascript" src="http://flesler-plugins.googlecode.com/files/jquery.rule-1.0.1.js"></script>
  <script type="text/javascript" src="http://remysharp.com/time.js"></script>

  <script type="text/javascript">
    function changeStyles1() {
      time.start("changeStyles1");
      $("div.dv").css("background-color", "Silver");
      $("a.lnk").css("color", "Black");
      time.stop("changeStyles1");
      time.report();
    }
    function changeStyles2() {
      time.start("changeStyles2");
      var html = "<style type='text/css'>.dv{background-color: Silver;} .lnk{color: Black;}</style>";
      $("div#styles").html(html);
      time.stop("changeStyles2");
      time.report();
    }
    function changeStyles3() {
      time.start("changeStyles3");
      var html = "<style type='text/css'>.dv{background-color: Silver;} .lnk{color: Black;}</style>";
      $("div#styles").html(html);
      time.stop("changeStyles3");
      time.report();
    }
  </script>

</head>
<body>
  <div id="styles">
    <style type="text/css">
      .dv
      {
        background-color: Yellow;
      }
      .lnk
      {
        color: Red;
      }
    </style>
  </div>

  Change styles using:
  <button onclick="changeStyles1();">
    jquery selectors</button>
  <button onclick="changeStyles2();">
    global styles</button>
  <button onclick="changeStyles3();">
    jQuery.rule</button>

  <div id="container">
    <div class='dv'>div # 0 <br/><a href='about:blank' class='lnk'>Link # 0</a></div>
    <div class='dv'>div # 1 <br/><a href='about:blank' class='lnk'>Link # 1</a></div>
    <div class='dv'>div # 2 <br/><a href='about:blank' class='lnk'>Link # 2</a></div>
    <div class='dv'>div # 3 <br/><a href='about:blank' class='lnk'>Link # 3</a></div>
    <div class='dv'>div # 4 <br/><a href='about:blank' class='lnk'>Link # 4</a></div>
    <div class='dv'>div # 5 <br/><a href='about:blank' class='lnk'>Link # 5</a></div>
...

  </div>
</body>
</html>

Example:
Change styles using:
div # 0
Link # 0
div # 1
Link # 1
div # 2
Link # 2
div # 3
Link # 3
div # 4
Link # 4
div # 5
Link # 5

Then I tested running time in IE7 and Mozilla 3.5:
jQuery selectorsglobal stylesjQuery.rule
IE72834 ms15 ms17 ms
Mozilla 1108 ms 10 ms6 ms

Personally I prefer "global styles" approach cause in case when one has to change many styles at once it works faster than other methods and forces browser to update its stylesheet collection only once.
Used libraries:

Monday, October 12, 2009

First post

For a long time I wanted to open my own blog but because of lack of time couldn't do that until now. Congratulations, first step is done!
Hopefully I'll keep this blog updated in the future. lets see...