3dz Soft

Author Archive

Python – Search key in a Dictionary by value

by on Feb.13, 2012, under Code snippets

If you need to get the key of an item from a Dictionary in Python, supposing that the items are unique, here’s an easy way to do it:


def find_key(dic, val):
 """return the key of dictionary dic given the value"""
 return [k for k, v in dic.iteritems() if v == val][0]

where dic is the dictionary, and val is the value for  whose key you are looking for.

Leave a Comment : more...

Get disk serial for licensing system

by on Feb.13, 2012, under Misc

I’m currently working on a big project, a management application for Desktop PCs. I had to implement a licensing system, so I chose to make the licensing based on the hard-disk’s serial, which is an unique number , and can’t be modified. To obtain the serial I preferred to use a library named GetDiskSerial. It consists of a DLL that can be easily used in Delphi, C++Builder, C#, Visual C++, Visual Basic, etc. Also, there are examples of how to use this library in all of these programming languages. The pricing is pretty reasonable, starting from $39.95, for a personal license. The price might go up to $359.55 for an unlimited license, but most of the time an commercial license that costs $69.95 will do.

I hope that this library will come as handy for you as it came to me :)

Happy unbreakable licensing systems coding!

Leave a Comment :, more...

C# – Copying a file with the same name without overwriting

by on May.29, 2011, under Code snippets

I recently encountered a situation where I needed to copy some files over a location where files with the same name already existed, and overwriting them wasn’t an option. So, the following code snippet verifies if a file with the same name already exists, in case which it tries to put a number after the file’s name.

public static void FileCopyWithoutOverwriting(string sourceFile, string fileName, string destinationPath)
        {
            // if destinationPath doesn't add with a slash, add one
            if ((destinationPath.EndsWith("\\") || destinationPath.EndsWith("/")) == false)
                destinationPath  += "\\";

            try
            {
                // if file already exists in destination
                if (File.Exists(destinationPath + fileName))
                {
                    // counter
                    int count = 1;

                    // extract extension
                    FileInfo info = new FileInfo(sourceFile);
                    string ext = info.Extension;
                    string prefix;

                    // if it has an extension, append it to a .
                    if (ext.Length > 0)
                    {
                        // get filename without extension
                        prefix = fileName.Substring(0, fileName.Length - ext.Length);
                        ext = "." + ext;
                    }
                    else
                        prefix = fileName;

                    // while not found an valid destination file name, increase counter
                    while (File.Exists(destinationPath + fileName))
                    {
                        fileName = prefix + "(" + count.ToString() + ")" + ext;
                        count++;
                    }
                    // copy file
                    File.Copy(sourceFile, destinationPath + fileName);
                }
                else
                {
                    File.Copy(sourceFile, destinationPath + fileName);
                }
                Console.WriteLine("File copy done.");
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("The caller does not have the required permission.");
            }
            catch (ArgumentException)
            {
                Console.WriteLine("One of the arguments is invalid.");
            }
            catch (PathTooLongException)
            {
                Console.WriteLine("The specified path, file name, or both exceed the system-defined maximum length.");
            }
            catch (DirectoryNotFoundException)
            {
                Console.WriteLine("At least one of the specified paths are invalid.");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("{0} was not found", sourceFile);
            }
            catch (IOException)
            {
                Console.WriteLine("An I/O error has occurred.");
            }
            catch (NotSupportedException)
            {
                Console.WriteLine("Source filename is in invalid format.");
            }
        }

I hope that you’ll find this helpful :)

UPDATE: A made a better version, with exception  handling too.

2 Comments more...

How to import books to Aldiko

by on Feb.09, 2011, under Android

If you want to import your own ebooks to Aldiko, one of the best ebook readers on Android, this is what you have to do:

  • Install some ebook management software on your PC, for example Calibre
  • Import your ebooks and convert them to epub format
  • Copy the epub files to your Android smartphone to phone_directory/ebooks/import ; if the folder doesn’t exists, create it
  • Start Aldiko, press the menu key, select Import, and wait until the files are copied

Enjoy!

Leave a Comment :, , , more...

How to change GRUB default entry

by on Jan.31, 2011, under Linux

If you installed Linux over Windows, the bootloader will change to GNU GRUB, and it’s default entry will be the fresh installed Linux. If you don’t want that, and you want Windows to be the default entry, this is what you have to do:

  • in GRUB, count which line is the entry which you want to make it default; the count starts from 0, so the second line is 1, the third is 2 etc.
  • boot into Linux
  • open Terminal
  • run the following command: sudo gedit /boot/grub/grub.cfg
  • if prompted, enter your password
  • in gedit, find the following line: set default=”0″
  • change “0″ to the line you want
  • save the file, and exit.
  • you’re done :)

If you don’t want to use GRUB at all, you can try EasyBCD (runs only on Windows), it’s a very good boot manager.

1 Comment :, , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!