Posts
[C#]如何取出最近在Windows上所使用的文件檔案
return targetFile; } public static IEnumerable<string> GetRecentlyFiles() { var recentFolder = Environment.GetFolderPath(Environment.SpecialFolder.Recent); return from file in Directory.EnumerateFiles(recentFolder) where Path.GetExtension(file) == ".lnk" select GetShortcutTargetFile(file); } }</pre></div> namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { listBox1.Items.Clear(); foreach (var file in RecentlyFileHelper.GetRecentlyFiles()) { listBox1.Items.Add(file); } var recentFolder = Environment.GetFolderPath(Environment.SpecialFolder.Recent); fileSystemWatcher1.Path = recentFolder; fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(fileSystemWatcher1_Created); } void fileSystemWatcher1_Created(object sender, System.
read morePosts
[C#]如何取得Process的Owner
if (processObj == null) throw new ArgumentException("Process not exists!"); var argList = new string[2]; int returnVal = Convert.ToInt32(processObj.InvokeMethod("GetOwner", argList)); if (returnVal == 0) { return string.Join(@"\", argList.Reverse().ToArray()); } return null; }</pre></div> if (processObj == null) throw new ArgumentException("Process not exists!"); var argList = new string[2]; int returnVal = Convert.ToInt32(processObj.InvokeMethod("GetOwner", argList)); if (returnVal == 0) { return string.Join(@"\", argList.Reverse().ToArray()); } return null; } }
read morePosts
[C#]如何在程式中內嵌其它應用程式
if (HideApplicationTitleBar) SetWindowLong(handle, GWL_STYLE, WS_VISIBLE);
SetParent(handle, this.Handle);
MoveWindow(handle, 0, 0, this.Width, this.Height, true); …
if (handle != IntPtr.Zero) MoveWindow(handle, 0, 0, this.Width, this.Height, true); }</pre>
read morePosts
[C#]如何為程式加上Windows的SendTo功能支援
private static void CreateShortCut(string shortCutFile, string targetPath, string description = "") { var type = Type.GetTypeFromProgID("WScript.Shell"); object instance = Activator.CreateInstance(type); var result = type.InvokeMember("CreateShortCut", BindingFlags.InvokeMethod, null, instance, new object[] { shortCutFile }); type = result.GetType(); type.InvokeMember("TargetPath", BindingFlags.SetProperty, null, result, new object[] { targetPath }); type.InvokeMember("Description", BindingFlags.SetProperty, null, result, new object[] { description }); type.InvokeMember("Save", BindingFlags.InvokeMethod, null, result, null); } private static void CreateSendToShortCut(string shortCutFileName, string targetPath, string description = "") { var sendToFolderPath = Environment.
read morePosts
[C#]如何解決在Vista以後開啟檔案FileSystemWatcher無法觸發LastAccess的問題
static void fw_Changed(object sender, FileSystemEventArgs e) { }</pre>
read morePosts
[C#]實作UDP Broadcast的傳送與接收
private static void BroadcastMessage(byte[] message, int port) { using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { sock.EnableBroadcast = true; sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); var iep = new IPEndPoint(IPAddress.Broadcast, port); sock.SendTo(message, iep); } }</pre></div> private void ReceiveBroadcastMessage(Action<EndPoint, byte[]> receivedAction, int port) { using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { var ep = new IPEndPoint(IPAddress.Any, port) as EndPoint; sock.Bind(ep);
while (true) { var buffer = new byte[1024]; var recv = sock.
read morePosts
[C#]將指定的檔案刪除並送到資源回收桶
#region Dllimport /// <summary> /// SHs the file operation. /// </summary> /// <param name="FileOp">The file op.</param> /// <returns></returns> [DllImport("shell32.dll", CharSet = CharSet.Auto)] public static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp); #endregion #region Const public const int FO_DELETE = 3; public const int FOF_ALLOWUNDO = 0x40; public const int FOF_NOCONFIRMATION = 0x10; #endregion #region Public Static Method public static void DeleteFileToRecyclebin(string file, Boolean showConfirmDialog = false) { var shf = new SHFILEOPSTRUCT(); shf.
read more