Accessing the X509Store in Silverlight - c#

I'm currently developing a Silverlight app in order to access the X509Store to sign data with a private key embedded into a USB Security Token.
I started from a C# application that works this way :
public byte[] SignData(byte[] HashTosign, string Cert_To_Use_b64)
{
byte[] Signature = null;
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
store.Close();
try {
foreach (X509Certificate2 Certificate in collection) {
if (ToBase64(Certificate.RawData) == Cert_To_Use_b64) {
RSACryptoServiceProvider Rsa = new RSACryptoServiceProvider();
Rsa = (RSACryptoServiceProvider)Certificate.PrivateKey;
try {
RSACryptoServiceProvider aesRsa = new RSACryptoServiceProvider();
string strPk = Certificate.PrivateKey.ToXmlString(true);
aesRsa.FromXmlString(strPk);
Signature = aesRsa.SignHash(HashTosign, CryptoConfig.MapNameToOID("SHA256"));
} catch (Exception ex) {
Throw new exception("FAILURE : " + ex.Message());
}
lgSignature = Signature.Length;
return 0;
}
}
} catch (CryptographicException ex) {
Throw new exception("FAILURE : " + ex.Message());
} catch (Exception ex) {
Throw new exception("FAILURE : " + ex.Message());
}
}
That method works great in a C# application. But when I try to adapt it to Silverlight, the X509Store doesn't seem to be implemented.
The name “X509Store” does not exist in the namespace System.Security.Cryptography. Are you missing an assembly reference?
I tried to apply a .NET Framework DLL but I got the following error :
It is not possible to add a reference to System.Security.dll because it was not created with the Silverlight runtime. Silverlight projects only work with Silverlight assemblies.
Can I recreate the System.Security.dll assembly to Silverlight ? Or is there a better means to do what I want to do ?
Thanks in advance.

There are two possible ways to do what you want:
CryptoAPI via P/Invoke
My fist guess was to use native Windows library crypt32.dll (commonly known as CryptoApi). After all System.Security.dll is just a managed wrapper around it (browsing decompiled source with tool like ILSpy will show you how MS is wrapping it). You can use it in Silverlight with P/Invoke when running in elevated trust (Properties -> Silverlight -> Require elevated trust) and with administrator rights (to be able to open certificate store). However there are couple of drawbacks:
It is rather unfriendly (brings me back old good C/C++ days)
You need to operate on low level system structures and operations (like pointers)
You will need to use this style for both certificate retrieval and signature computing
Documentation reference:
MSDN CryptoApi
This way I managed to acquire handle (IntPtr pointer) to certificate enrolled on my smart card:
using System;
using System.Runtime.InteropServices;
namespace SilverlightX509Store
{
public static class CapiNative
{
public const string MY = "MY";
public const uint PKCS_7_ASN_ENCODING = 0x00010000;
public const uint X509_ASN_ENCODING = 0x00000001;
public const uint CERT_FIND_SUBJECT_STR = 0x00080007;
public const int ACCESS_DENIED = 5;
[DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CertOpenSystemStore(
IntPtr hCryptProv,
string storename);
[DllImport("crypt32.dll", SetLastError = true)]
public static extern IntPtr CertFindCertificateInStore(
IntPtr hCertStore,
uint dwCertEncodingType,
uint dwFindFlags,
uint dwFindType,
[In, MarshalAs(UnmanagedType.LPWStr)]String pszFindString,
IntPtr pPrevCertCntxt);
}
public class CapiWrapper
{
public IntPtr FindCert(string subject)
{
IntPtr storeHandle = CapiNative.CertOpenSystemStore(
IntPtr.Zero,
CapiNative.MY);
if (Marshal.GetLastWin32Error() == CapiNative.ACCESS_DENIED)
{
return IntPtr.Zero;
}
IntPtr certHandle = CapiNative.CertFindCertificateInStore(
storeHandle,
CapiNative.PKCS_7_ASN_ENCODING | CapiNative.X509_ASN_ENCODING,
0,
CapiNative.CERT_FIND_SUBJECT_STR,
"subject to find",
IntPtr.Zero);
return certHandle;
}
}
}
Converting .NET dll to Silverlight compliant
There seems to be a way to convert .NET library into SL. See article Reusing .NET Assemblies in Silverlight.
I haven't tested it yet. Potential issue might be that .NET security end encryption is built in deep into mscorlib and System.Security and it might not be so easy to convert those libraries (or even not possible at all).

Related

Dot net core C# open photo viewer with Process Class

I am going to open the photo viewer using .net core and this is my code
using System.Diagnostics;
namespace TestProcessForOpenPhoto
{
class Program
{
static void Main(string[] args)
{
var photoViewer = new Process();
photoViewer.StartInfo.FileName = #"C:\Program Files\Windows Photo Viewer\PhotoViewer.dll";
photoViewer.StartInfo.Arguments = #" C:\Users\XXXXX\Desktop\TestImage\abc.jpg";
photoViewer.StartInfo.UseShellExecute = false;
photoViewer.Start();
}
}
}
and I got this error message
System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.'
Can anyone help me to fix this bug, thanks
After researching this I noticed folks using rundll32.exe to execute an export from PhotoViewer.dll to display a picture using Microsoft Photo Viewer application. So I think that's what OP was trying to do, they just forgot to use the rundll32.exe application.
So I thought I'd take a crack at this and not use the rundll32.exe and just call the export directly. I debugged it with x86dbg and saw that it's passing in 4 parameters: pointer, pointer, pointer (to wchar_t*), int. I don't know what the parameters do, so I just set them to NULL and made sure to pass in the path to the picture as the 3rd and it seems to work.
So this will do what you want it to do. I know that hard-coding system paths is bad practice, but maybe someone who has more time can make this more dynamic.
private static class WindowsPhotoViewer
{
private const string FilePath32 = #"c:\program files (x86)\Windows Photo Viewer\PhotoViewer.dll";
private const string FilePath64 = #"c:\program files\Windows Photo Viewer\PhotoViewer.dll";
[DllImport(FilePath32, CharSet = CharSet.Unicode, EntryPoint = "ImageView_FullscreenW")]
private static extern void ImageView_Fullscreen32(
IntPtr unknown1, IntPtr unknown2, string path, int unknown3);
[DllImport(FilePath64, CharSet = CharSet.Unicode, EntryPoint = "ImageView_FullscreenW")]
private static extern void ImageView_Fullscreen64(
IntPtr unknown1, IntPtr unknown2, string path, int unknown3);
public static bool ShowImage(FileInfo imageFile)
{
if ((IntPtr.Size == 8) && File.Exists(FilePath64) && imageFile.Exists)
{
ImageView_Fullscreen64(IntPtr.Zero, IntPtr.Zero, imageFile.FullName, 0);
return true;
}
else if ((IntPtr.Size == 4) && File.Exists(FilePath32) && imageFile.Exists)
{
ImageView_Fullscreen32(IntPtr.Zero, IntPtr.Zero, imageFile.FullName, 0);
return true;
}
return false;
}
}
Then you can call it as so:
if(!WindowsPhotoViewer.ShowImage(new FileInfo(#"c:\users\andy\desktop\test.jpg")))
{
Console.WriteLine("Failed to show image");
}

How to transfer files to a shared folder using IP address in C#

I am new in C#, i am having a PowerShell Script to send files to multiple PC using IP address and username, there I am using new-PSDrive. I want to create the same program in C#.
I am not sure how to do that i went through some tutorials and tried it out but stuck with Windows Impersonate Class. It was written in the post that i followed that: _If we want to share file to a shared folder we can use File.Copy(destPath, SourcePath) but its not working.
This is the Code i am trying :
WindowsIdentity idnt = new WindowsIdentity("Administrator", "Test123!");
WindowsImpersonationContext context = idnt.Impersonate();
File.Copy(#"C:\\Sent.txt", #"\\192.xxx.xxx.xxx\\SharedFolder", true);
context.Undo();
An error Pop's up : The name provided is not a properly formed account name.
WindowsIdentity idnt = new WindowsIdentity("Administrator", "Test123!");
I don't know how to get proper name, i am trying this :
WindowsIdentity idnt = new WindowsIdentity(Username,Password);
I also trien this ("\192.xxx.xxx.xxx\WIN-9SMSBCR4V7B\SharedFolder",Password);
The Machine on which i want to copy files is running on Vmware on same machine and i am able to send using Powershell Script.
Any suggestion would be appreciated.
Found the Solution, Here's the complete code for sending files to a remote PC using its IP address, UserName and Password for machines which are not on same DOMAIN.
Here the Link which explains the use of correct LOGON PROVIDER
using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Security.Principal;
using System.IO;
namespace File_Send_Test
{
class Program
{
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
// Test harness.
// If you incorporate this code into a DLL, be sure to demand FullTrust.
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public static void Main(string[] args)
{
SafeTokenHandle safeTokenHandle;
try
{
string userName, domainName;
//domainName = Console.ReadLine();
domainName = ".";
Console.Write("Enter the login of a user on {0} that you wish to impersonate: ", domainName);
//provide username of remote machine.
userName = Console.ReadLine();
//provide password of remote machine.
Console.Write("Enter the password for {0}: ", userName);
//Here's the Catch
//LOGON32_PROVIDER_WinNT50 = 3; and LOGON32_LOGON_NewCredentials = 9;
const int LOGON32_PROVIDER_WinNT50 = 3;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_NewCredentials = 9;
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(userName, domainName, Console.ReadLine(),
LOGON32_LOGON_NewCredentials, LOGON32_PROVIDER_WinNT50,
out safeTokenHandle);
Console.WriteLine("LogonUser called.");
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
Console.WriteLine("LogonUser failed with error code : {0}", ret);
throw new System.ComponentModel.Win32Exception(ret);
}
using (safeTokenHandle)
{
Console.WriteLine("Did LogonUser Succeed? " + (returnValue ? "Yes" : "No"));
Console.WriteLine("Value of Windows NT token: " + safeTokenHandle);
// Check the identity.
Console.WriteLine("Before impersonation: "
+ WindowsIdentity.GetCurrent().Name);
// Use the token handle returned by LogonUser.
using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
{
using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
{
// Check the identity.
Console.WriteLine("After impersonation: "
+ WindowsIdentity.GetCurrent().Name);
//File.Copy(Source File,DestinationFile);
File.Copy(#"C:\\Sent.txt", #"\\192.168.xxx.xxx\\Suji\\Sent.txt", true);
}
}
// Releasing the context object stops the impersonation
// Check the identity.
Console.WriteLine("After closing the context: " + WindowsIdentity.GetCurrent().Name);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception occurred. " + ex.Message);
}
Console.ReadLine();
}
}
public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private SafeTokenHandle()
: base(true)
{
}
[DllImport("kernel32.dll")]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr handle);
protected override bool ReleaseHandle()
{
return CloseHandle(handle);
}
}
}
Hope this would help others.
You would have thought copying a file from one folder to a remote share (which needs a user name / password) would be simple ! But, it is not !
The following 2 links provide some options:
(1)
How to provide user name and password when connecting to a network share
The above links is about mapping a network drive first and then doing a file copy
(2) copy files with authentication in c#
This option is about using the WindowsIdentity class (as you have attempted). The above link gives a way to construct the object in a correct way
Both of the above options, in a way, are NOT pure .Net solutions. They call Win32 APIs directly.
Another option:
If you could have a mapped network connection created (outside of your application) first, then a simple file copy would work.
In that case the steps would be:
(1) Map a drive to the shared folder using an user name and password, using the net use command
net use Z: /delete
net use Z: \\server name\share name password /user:user name
(2) Run your copy program
File.Copy(sourceFileName, #"Z:\path\to\folder\filename");
(3) Remove the drive mapping
net use Z: /delete

Is it possible to send Toast notification from console application?

Is it possible to send Toast notifications from console application using ToastNotificationManager ?
I know that it is possible to send Toast notifications from Windows Universal app:
var toast = new ToastNotification(doc);
ToastNotificationManager.CreateToastNotifier().Show(toast);
*doc - Toast stored in XML string
To use ToastNotificaionManager I need Windows.UI.Notifications library which I can't reference in console application project.
The library I mentionet before is actualy used by WinRT. Is it possible to use WinRT APIs in Windows console application ?
At first you need to declare that your program will be using winRT libraries:
Right-click on your yourProject, select Unload Project
Right-click on your yourProject(unavailable) and click Edit yourProject.csproj
Add a new property group:<targetplatformversion>8.0</targetplatformversion>
Reload project
Add reference Windows from Windows > Core
Now you need to add this code:
using Windows.UI.Notifications;
and you will be able to send notifications using this code:
var toast = new ToastNotification(doc);
ToastNotificationManager.CreateToastNotifier().Show(toast);
Reference: How to call WinRT APIs in Windows 8 from C# Desktop Applications - WinRT Diagram
I ran into some problems here with Evaldas B's Code I was missing a string.
(Where It Says Need String Here)
.CreateToastNotifier(<needed a string here>).Show(toast);
warning I am kind of new to C# so my code probably sucks- but it does work and is pretty simplistic and that's more than I can say for most solutions I have found
Also I was having a hell of a time getting the xml document to read. I was fighting with System.xml (I think) and Windows.Data.Dom.Xml (also not completely sure).
In the end I settled on making them hard coded strings for my example file and used a switch statement to switch between them.
I have found a ton of people, looking for the solution that I have come up with, on stack overflow. It seems use of the toast notification system with console or background applications would be super useful, and the documentation that surrounds the toast notification system with windows applications all suggest that it needs to be used with an application. The Action Center is super useful for notifications vrs the NotificationTray/NotifyIcon route. I have not found a full solution anywhere else on the web. Here is example code.
/*
At first you need to declare that your program will be using winRT libraries:
1. Right click on your yourProject, select Unload Project
2. Right click on your youProject(unavailable) and click Edit yourProject.csproj
3. Add a new property group:<TargetPlatformVersion>8.0</TargetPlatformVersion>
4. Reload project
5. Add referece Windows from Windows > Core
*/
using System;
using Windows.Data.Xml.Dom;
using Windows.Storage;
using Windows.Storage.Streams;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Notifications;
namespace ConsoleApplication6
{
public class NewToastNotification
{
public NewToastNotification(string input, int type)
{
string NotificationTextThing = input;
string Toast = "";
switch (type)
{
case 1:
{
//Basic Toast
Toast = "<toast><visual><binding template=\"ToastImageAndText01\"><text id = \"1\" >";
Toast += NotificationTextThing;
Toast += "</text></binding></visual></toast>";
break;
}
default:
{
Toast = "<toast><visual><binding template=\"ToastImageAndText01\"><text id = \"1\" >";
Toast += "Default Text String";
Toast += "</text></binding></visual></toast>";
break;
}
}
XmlDocument tileXml = new XmlDocument();
tileXml.LoadXml(Toast);
var toast = new ToastNotification(tileXml);
ToastNotificationManager.CreateToastNotifier("New Toast Thing").Show(toast);
}
}
class Program
{
static void Main(string[] args)
{
NewToastNotification Window = new NewToastNotification("Yes",1);
}
}
}
1) For a toast notification to appear using a console or Desktop application, your application must have a shortcut on the start menu.
2) For an application to have a shortcut icon(not tile icon) in the start menu of Windows, your app must have an AppId.
To create a short cut for you you application create a new class named ShellHelpers.cs and Paste this code in it.
using System;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
using MS.WindowsAPICodePack.Internal;
namespace DesktopToastsSample.ShellHelpers
{
internal enum STGM : long
{
STGM_READ = 0x00000000L,
STGM_WRITE = 0x00000001L,
STGM_READWRITE = 0x00000002L,
STGM_SHARE_DENY_NONE = 0x00000040L,
STGM_SHARE_DENY_READ = 0x00000030L,
STGM_SHARE_DENY_WRITE = 0x00000020L,
STGM_SHARE_EXCLUSIVE = 0x00000010L,
STGM_PRIORITY = 0x00040000L,
STGM_CREATE = 0x00001000L,
STGM_CONVERT = 0x00020000L,
STGM_FAILIFTHERE = 0x00000000L,
STGM_DIRECT = 0x00000000L,
STGM_TRANSACTED = 0x00010000L,
STGM_NOSCRATCH = 0x00100000L,
STGM_NOSNAPSHOT = 0x00200000L,
STGM_SIMPLE = 0x08000000L,
STGM_DIRECT_SWMR = 0x00400000L,
STGM_DELETEONRELEASE = 0x04000000L,
}
internal static class ShellIIDGuid
{
internal const string IShellLinkW = "000214F9-0000-0000-C000-000000000046";
internal const string CShellLink = "00021401-0000-0000-C000-000000000046";
internal const string IPersistFile = "0000010b-0000-0000-C000-000000000046";
internal const string IPropertyStore = "886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99";
}
[ComImport,
Guid(ShellIIDGuid.IShellLinkW),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IShellLinkW
{
UInt32 GetPath(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
int cchMaxPath,
//ref _WIN32_FIND_DATAW pfd,
IntPtr pfd,
uint fFlags);
UInt32 GetIDList(out IntPtr ppidl);
UInt32 SetIDList(IntPtr pidl);
UInt32 GetDescription(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
int cchMaxName);
UInt32 SetDescription(
[MarshalAs(UnmanagedType.LPWStr)] string pszName);
UInt32 GetWorkingDirectory(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir,
int cchMaxPath
);
UInt32 SetWorkingDirectory(
[MarshalAs(UnmanagedType.LPWStr)] string pszDir);
UInt32 GetArguments(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs,
int cchMaxPath);
UInt32 SetArguments(
[MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
UInt32 GetHotKey(out short wHotKey);
UInt32 SetHotKey(short wHotKey);
UInt32 GetShowCmd(out uint iShowCmd);
UInt32 SetShowCmd(uint iShowCmd);
UInt32 GetIconLocation(
[Out(), MarshalAs(UnmanagedType.LPWStr)] out StringBuilder pszIconPath,
int cchIconPath,
out int iIcon);
UInt32 SetIconLocation(
[MarshalAs(UnmanagedType.LPWStr)] string pszIconPath,
int iIcon);
UInt32 SetRelativePath(
[MarshalAs(UnmanagedType.LPWStr)] string pszPathRel,
uint dwReserved);
UInt32 Resolve(IntPtr hwnd, uint fFlags);
UInt32 SetPath(
[MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
[ComImport,
Guid(ShellIIDGuid.IPersistFile),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IPersistFile
{
UInt32 GetCurFile(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile
);
UInt32 IsDirty();
UInt32 Load(
[MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
[MarshalAs(UnmanagedType.U4)] STGM dwMode);
UInt32 Save(
[MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
bool fRemember);
UInt32 SaveCompleted(
[MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
}
[ComImport]
[Guid(ShellIIDGuid.IPropertyStore)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IPropertyStore
{
UInt32 GetCount([Out] out uint propertyCount);
UInt32 GetAt([In] uint propertyIndex, out PropertyKey key);
UInt32 GetValue([In] ref PropertyKey key, [Out] PropVariant pv);
UInt32 SetValue([In] ref PropertyKey key, [In] PropVariant pv);
UInt32 Commit();
}
[ComImport,
Guid(ShellIIDGuid.CShellLink),
ClassInterface(ClassInterfaceType.None)]
internal class CShellLink { }
public static class ErrorHelper
{
public static void VerifySucceeded(UInt32 hresult)
{
if (hresult > 1)
{
throw new Exception("Failed with HRESULT: " + hresult.ToString("X"));
}
}
}
}
Code for creating a shortcut(This code can be added to the same class where you will be showing the toast)
public bool TryCreateShortcut()
{
String shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Windows\\Start Menu\\Programs\\FixSus Toasts Sample .lnk";
if (!File.Exists(shortcutPath))
{
InstallShortcut(shortcutPath);
return true;
}
return false;
}
private void InstallShortcut(String shortcutPath)
{
// Find the path to the current executable
String exePath = Process.GetCurrentProcess().MainModule.FileName;
IShellLinkW newShortcut = (IShellLinkW)new CShellLink();
// Create a shortcut to the exe
DesktopToastsSample.ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath));
DesktopToastsSample.ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetArguments(""));
// Open the shortcut property store, set the AppUserModelId property
IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut;
using (PropVariant appId = new PropVariant(APP_ID))
{
DesktopToastsSample.ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId));
DesktopToastsSample.ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.Commit());
}
// Commit the shortcut to disk
IPersistFile newShortcutSave = (IPersistFile)newShortcut;
DesktopToastsSample.ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true));
}
Now you can create an show a toast
// Get a toast XML template
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements[1].AppendChild(toastXml.CreateTextNode("Message" + newMessage));
// Specify the absolute path to an image
string codeWebFolderPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, #"..\..\"));
String imagePath = "file:///" + Path.GetFullPath(codeWebFolderPath+ "Resources\\FixSus.png");
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;
// Create the toast and attach event listeners
ToastNotification toast = new ToastNotification(toastXml);
toast.Activated += ToastActivated;
toast.Dismissed += ToastDismissed;
toast.Failed += ToastFailed;
// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
The APP_ID can be any string. In my case it was "NotificationTest.KEY"
Note: Dont modify the ShellHelper class.
Edit : Follow Evaldas B's answer first then apply this solution.

ExtractAssociatedIcon returns null

I'm using the ExtractAssociatedIcon method to retrieve the icon for the file. My hope is to retrieve the same icon that a user would see in their explorer window.
public static Icon GetIcon(string fileName)
{
try
{
Icon icon = Icon.ExtractAssociatedIcon(fileName);
return icon;
}
catch
{
return null;
}
}
This works 99% of the time. However, if the user has linked to a file on a shared path, such as \\SOME_SERVER\my documents\this file.pdf it returns null. It falls through the "catch" with the error that the file path is not a valid path.
It is a valid URI (I've verified the file exists, is readable, etc.), but not a valid fully-qualified drive path with the X:\some\folder notation.
How can I get around this, if at all?
Thanks.
Re-UPDATE
Here's the solution I ended up with. It's much cleaner than the first update. Many thanks to Chris Haas, whose answer was a comment, and not a direct answer. If/when he makes it a direct answer, I will update this as such.
I still had to go down to a lower level and fetch the icon through C++ libraries, but the only library I needed is listed below:
#region Old-School method
[DllImport("shell32.dll")]
static extern IntPtr ExtractAssociatedIcon(IntPtr hInst,
StringBuilder lpIconPath, out ushort lpiIcon);
public static Icon GetIconOldSchool(string fileName)
{
ushort uicon;
StringBuilder strB = new StringBuilder(fileName);
IntPtr handle = ExtractAssociatedIcon(IntPtr.Zero, strB, out uicon);
Icon ico = Icon.FromHandle(handle);
return ico;
}
#endregion
Once I had defined the above method, the GetIcon() method becomes:
public static Icon GetIcon(string fileName)
{
try
{
Icon icon = Icon.ExtractAssociatedIcon(fileName);
return icon;
}
catch
{
try
{
Icon icon2 = GetIconOldSchool(fileName);
return icon2;
}
catch
{
return null;
}
}
}
(Comment turned into post - CTIP)
Check out the link here which eventually leads to P/Invoke.net with the following code:
[DllImport("shell32.dll")]
static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, StringBuilder lpIconPath, out ushort lpiIcon);
[DllImport("shell32.dll")]
static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);
_
ushort uicon;
StringBuilder strB = new StringBuilder(YOUR_FILE_PATH);
IntPtr handle = ExtractAssociatedIcon(this.Handle, strB, out uicon);
Icon ico = Icon.FromHandle(handle);
return ico.ToBitmap();

How to find available COM ports?

How to find available COM ports in my PC? I am using framework v1.1. Is it possible to find all COM ports? If possible, help me solve the problem.
Framework v1.1 AFAIK doesn't allow you to do this.
In 2.0 there is a static function
SerialPort.GetPortNames()
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames.aspx
As others suggested, you can use WMI. You can find a sample in CodeProject
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSSerial_PortName");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("MSSerial_PortName instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);
Console.WriteLine("-----------------------------------");
Console.WriteLine("MSSerial_PortName instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("PortName: {0}", queryObj["PortName"]);
//If the serial port's instance name contains USB
//it must be a USB to serial device
if (queryObj["InstanceName"].ToString().Contains("USB"))
{
Console.WriteLine(queryObj["PortName"] + "
is a USB to SERIAL adapter/converter");
}
}
}
catch (ManagementException e)
{
Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
}
The available serial ports can also be found at the values at the HKEY_LOCAL_MACHINE\hardware\devicemap\serialcomm key in the registry.
How about asking a straight question from operating system:
using System;
using System.Collections.Generic;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
public class MyClass
{
private const uint GENERIC_ALL = 0x10000000;
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint GENERIC_EXECUTE = 0x20000000;
private const int OPEN_EXISTING = 3;
public const int INVALID_HANDLE_VALUE = -1;
public static void Main()
{
for (int i = 1; i <= 32; i++)
Console.WriteLine ("Port {0}: {1}", i, PortExists (i));
}
private static bool PortExists (int number) {
SafeFileHandle h = CreateFile (#"\\.\COM" + number.ToString (), GENERIC_READ + GENERIC_WRITE,
0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
bool portExists = !h.IsInvalid;
if (portExists)
h.Close ();
return portExists;
}
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern SafeFileHandle CreateFile (string lpFileName, System.UInt32 dwDesiredAccess,
System.UInt32 dwShareMode, IntPtr pSecurityAttributes, System.UInt32 dwCreationDisposition,
System.UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);
}
WMI contains a lot of hardware information. Query for instances of Win32_SerialPort.
(OTOH I can't recall how much WMI query support was in .NET 1.1.)
There is no support for SerialPort communication in .net v1.1. The most common solution for this was to use the MSCOMMCTL active X control from a VB6.0 installation (import into your .net project as a COM component from the add reference dialog box).
In later versions the Serial Port support is available through the System.IO.Ports name space. Also please note there is no API which will get you the list of free ports.
You can get a list of all the port names and then try opening a connection. An exception occurs if the port is already in use.
Since you are using .net 1.1 one option is to use the AxMSCommLib control.
Here is a web page that assisted me in starting to use AxMSCommLib control. There is even a FindDevicePort() method listed that can be easily modified.
I have since switched to System.IO.Ports which appears to be much more robust.
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=320
Thanks
Joe
Use QueryDosDevice API function. This is a VB6 snippet:
ReDim vRet(0 To 255)
sBuffer = String(100000, 1)
Call QueryDosDevice(0, sBuffer, Len(sBuffer))
sBuffer = Chr$(0) & sBuffer
For lIdx = 1 To 255
If InStr(1, sBuffer, Chr$(0) & "COM" & lIdx & Chr$(0), vbTextCompare) > 0 Then
vRet(lCount) = "COM" & lIdx
lCount = lCount + 1
End If
Next
Maybe you will find this useful?
I am showing you a simple way to check all the COM ports in you PC. To get started follow these steps:
Create a WinForms application in Visual Studio.
Darg and drop a comboBox in your form and name it comboBoxCOMPORT
Copy the following code and paste after the public Form1() method (autogenerated).
private void Form1_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
comboBoxCOMPORT.Items.AddRange(ports);
}
Run the app and click on drop down arrow on the comboBox to reveal all the available COM PORTS.
The above method works for Edgeport USB-to-serial converters as well as virtual ports. I implemented this in my project and works smoothly. Let me know if I can provide any further assistance.

Categories