Visualizzazione post con etichetta c#. Mostra tutti i post
Visualizzazione post con etichetta c#. Mostra tutti i post

giovedì 9 luglio 2009

Get the version information of an extern assembly

We have more ways to get the information of an assembly, the most two I use are:
  1. Only for .NET assembly
    System.Reflection.Assembly AssemblyFileInfo = System.Reflection.Assembly.Load(FileAppPath);
    Console.WriteLine (AssemblyFileInfo.GetName().Version);

  2. For all .NET and native assembly
    FileVersionInfo AssemblyFileVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(FileAppPath);
    Console.WriteLine(AssemblyFileVersion.FileVersion)

martedì 9 giugno 2009

Application Path

Sometime I need to retrieve the application path, here I add the four methods I use frequently.

  1. The simple way through windows forms:
    using
    System.Windows.Forms;
    String sAppPath1 = Application.StartupPath;
  2. From the binary over reflection:
    String sAppPath2 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
  3. Using the file informations:
    FileInfo executableFileInfo = new FileInfo (Application.ExecutablePath);
    String sAppPath3 = executableFileInfo.DirectoryName;
  4. When my program is started from another:
    System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Happy coding.

sabato 13 dicembre 2008

martedì 25 novembre 2008

File attributes with C#

The class I've posted shows how to use the fiel attributes with c#.
To get file attributes (in .Net) you need more steps, with this class you have immediatly all you need.
I covered the 4 standard attribute but you can extend the class with other attributes. The file is .called FileAttributeEx.cs and this it the code.

using
System;
using System.IO;

namespace FileAttributeEx
{
class FileAttributesEx
{
private string filepath = string.Empty;

public string FilePath
{
get { return filepath; }
set { filepath = value;}
}

public FileAttributesEx() { }
public FileAttributesEx(string path)
{
FilePath = path;
}

public bool isReadOnly ()
{
return (File.GetAttributes(FilePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
}

public bool isHidden()
{
return (File.GetAttributes(FilePath) & FileAttributes.Hidden) == FileAttributes.Hidden;
}

public bool isArchive()
{
return (
File.GetAttributes(FilePath) & FileAttributes.Archive) == FileAttributes.Archive;
}

public bool isSystem()
{
return (File.GetAttributes(FilePath) & FileAttributes.System) == FileAttributes.System;
}

public void AddSystem()
{
File.SetAttributes(FilePath, File.GetAttributes(FilePath) | FileAttributes.System);
}

public void AddReadOnly()
{
File.SetAttributes(FilePath, File.GetAttributes(FilePath) | FileAttributes.ReadOnly);
}

public void AddHidden()
{
File.SetAttributes(FilePath, File.GetAttributes(FilePath) | FileAttributes.Hidden);
}

public void AddArchive()
{
File.SetAttributes(FilePath, File.GetAttributes(FilePath) | FileAttributes.Archive);
}

public void Clear()
{
File.SetAttributes(filepath, File.GetAttributes(filepath)
& ~(FileAttributes.Archive |
FileAttributes.ReadOnly| FileAttributes.System|FileAttributes.Hidden));
}
}
}

To use it in your project simply, add the file to your project, add a new instance of it and call the metods you need.
Here a sample:

FileAttributesEx fileAttr = new FileAttributesEx(sFileName);

if (fileAttr.isArchive())
//Do something
if (fileAttr.isHidden())
//Do something

fileAttr.Clear();

fileAttr.AddReadOnly();

Have fun.

martedì 4 novembre 2008

Windows Mouse Control

With this class you can easy control the mouse from c#. For the click event it's use the P/Invoke of mouse_event API. To move the cursor positon use directly the managed code.
(Only for x86.)

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MouseMov
{
public class MouseHook
{
[DllImport("user32.dll")]
private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;

internal static void SendDoubleClick()
{
SendClick();
SendClick();
}

internal static void SendClick()
{
SendDown();
SendUp();
}

internal static void SendUp()
{
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
}
private void MoveMouse(Point p)
{
Cursor.Position = new Point(Cursor.Position.X + p.X, Cursor.Position.Y + p.Y);
}


internal static void SendDown()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
}
}
}

Have fun.