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.
Iscriviti a:
Post (Atom)