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ì 25 novembre 2008
venerdì 14 novembre 2008
How to convert string to byte array and viceversa
The way to convert a string to byte array are a lot. Here I show you one I use over all, UnicodeEncoding.
Sample:
char[] arrUnicodeSample = new char[] { '\uAAAA', '\uBBBB', '\uCCCC', '\uDFFF' };
string strUnicode = new string(arrUnicodeSample);
{
UnicodeEncoding encUnicode = new UnicodeEncoding ();
byte[] encodedBytes = encUnicode.GetBytes(strUnicode);
String decodedString = encUnicode.GetString(encodedBytes, 0, encodedBytes.Length);
}
You can also use:
Sample:
char[] arrUnicodeSample = new char[] { '\uAAAA', '\uBBBB', '\uCCCC', '\uDFFF' };
string strUnicode = new string(arrUnicodeSample);
{
UnicodeEncoding encUnicode = new UnicodeEncoding ();
byte[] encodedBytes = encUnicode.GetBytes(strUnicode);
String decodedString = encUnicode.GetString(encodedBytes, 0, encodedBytes.Length);
}
You can also use:
- Encoding
- ASCIIEncoding
- UnicodeEncoding
Etichette:
ASCIIEncoding,
Decoder,
Encoding,
UnicodeEncoding
giovedì 13 novembre 2008
My new article on Code Project: Mobile TouchPad
Finally I've found some free time to write a full article.
The article describe how to write a mobile program and show you how it's simple. In particular Mobile TouchPad lets you control your PC through your touch pad phones.
MobileTouchPad[^]
Have fun.
The article describe how to write a mobile program and show you how it's simple. In particular Mobile TouchPad lets you control your PC through your touch pad phones.
MobileTouchPad[^]
Have fun.
martedì 4 novembre 2008
How to convert class/struct to a byte array
With this class you can easy convert your class/struct to a byte array.
Very useful when using the socket.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
class CommonConvertion
{
public static byte[] StructureToByteArray(object obj)
{
int Length = Marshal.SizeOf(obj);
byte[] bytearray = new byte[Length];
IntPtr ptr = Marshal.AllocHGlobal(Length);
Marshal.StructureToPtr(obj, ptr, false);
Marshal.Copy(ptr, bytearray, 0, Length);
Marshal.FreeHGlobal(ptr);
return bytearray;
}
public static void ByteArrayToStructure(byte[] bytearray, ref object obj)
{
int Length = Marshal.SizeOf(obj);
IntPtr ptr = Marshal.AllocHGlobal(Length);
Marshal.Copy(bytearray, 0, ptr, Length);
obj = Marshal.PtrToStructure(ptr, obj.GetType());
Marshal.FreeHGlobal(ptr);
}
}
Have fun.
Very useful when using the socket.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
class CommonConvertion
{
public static byte[] StructureToByteArray(object obj)
{
int Length = Marshal.SizeOf(obj);
byte[] bytearray = new byte[Length];
IntPtr ptr = Marshal.AllocHGlobal(Length);
Marshal.StructureToPtr(obj, ptr, false);
Marshal.Copy(ptr, bytearray, 0, Length);
Marshal.FreeHGlobal(ptr);
return bytearray;
}
public static void ByteArrayToStructure(byte[] bytearray, ref object obj)
{
int Length = Marshal.SizeOf(obj);
IntPtr ptr = Marshal.AllocHGlobal(Length);
Marshal.Copy(bytearray, 0, ptr, Length);
obj = Marshal.PtrToStructure(ptr, obj.GetType());
Marshal.FreeHGlobal(ptr);
}
}
Have fun.
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.
(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.
martedì 30 settembre 2008
CSharp NumericTextBox control
I've post here a simple custom control that it accepts only numeric input.
The only one methods in the OnKeyPress (overrided) and accept only digit or the backspace
Have fun.
The only one methods in the OnKeyPress (overrided) and accept only digit or the backspace
using System;To use it you just drag & drop the control from the toolbox to your form.
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using DrLuiji.Controls
{
public class NumericTextBox : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (!((Char.IsDigit(e.KeyChar))||(e.KeyChar.Equals('\b'))))
{
e.Handled = true;
ErrorBeep();
}
}
public void ErrorBeep()
{
//Console.Beep();
}
}
}
Have fun.
Etichette:
Custom control,
NumericTextBox,
TextBox IsNumeric
lunedì 22 settembre 2008
Retrieve system folder c#
Here you can find a way to retrieve the system folder.
Very simple, call the System.Environment.GetFolderPath with the corrisponding enum System.Environment.SpecialFolder.See the sample below.
private string GetSystemSpecialFolderDirectory()
{
//Program files
string sProgramFilesFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ProgramFiles);
//Personal
string sPersonalFolder = System.Environment.GetFolderPath (
System.Environment.SpecialFolder.Personal);
//Desktop
string sDesktopFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.DesktopDirectory);
//Application Data
string sApplicationDataFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ApplicationData);
//System:
string sSystemFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.System);
MessageBox.Show("Program files: " + sProgramFilesFolder +
"\r\nPersonal: " + sPersonalFolder +
"\r\nDesktop: " + sDesktopFolder +
"\r\nApplication Data: " + sApplicationDataFolder +
"\r\nSystem: " + sSystemFolder);
}
Very simple isn't it ?
Very simple, call the System.Environment.GetFolderPath with the corrisponding enum System.Environment.SpecialFolder.See the sample below.
private string GetSystemSpecialFolderDirectory()
{
//Program files
string sProgramFilesFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ProgramFiles);
//Personal
string sPersonalFolder = System.Environment.GetFolderPath (
System.Environment.SpecialFolder.Personal);
//Desktop
string sDesktopFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.DesktopDirectory);
//Application Data
string sApplicationDataFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ApplicationData);
//System:
string sSystemFolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.System);
MessageBox.Show("Program files: " + sProgramFilesFolder +
"\r\nPersonal: " + sPersonalFolder +
"\r\nDesktop: " + sDesktopFolder +
"\r\nApplication Data: " + sApplicationDataFolder +
"\r\nSystem: " + sSystemFolder);
}
Very simple isn't it ?
Iscriviti a:
Post (Atom)

