Add this <Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" /> to the .csproj file.
http://forums.msdn.microsoft.com/en-US/wpf/thread/2f755d30-bd8c-4f9b-b36a-9cb56bea15cd/
-
Create .snk file
-
VS Command Prompt
-
sn -k [filename].snk
-
Copy .snk file into VS project
-
Include in project
-
Project -> Properties -> Signing -> Sign the assembly -> Select the .snk file
-
Add assembly to GAC through command-line or Installer
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseHandle(IntPtr hObject);
public enum LogonType { Interactive = 2, Network = 3, Batch = 4, Service = 5, Unlock = 7, NetworkClearText = 8, NewCredentials = 9 }
public
static WindowsImpersonationContext Impersonate(String domain, String user, String password, LogonType type) {
IntPtr userToken = IntPtr.Zero;
if(!LogonUser(user, domain, password, (int)type, 0, out userToken))
throw new Win32Exception(Marshal.GetLastWin32Error());
try {
return WindowsIdentity.Impersonate(userToken);
} finally {
CloseHandle(userToken);
}
}
static
void Main(string[] args) {
try {
using(WindowsImpersonationContext wic = Impersonate("DOMAIN", "User.Name", "Password", LogonType.Interactive)) {
// Do Something as new user
wic.Undo();
}
} catch(Exception ex) {
Console.Write(ex.ToString());
} finally {
Console.Read();
}
}
There are a couple of things I did to resolve this issue for me:
1. Exchange Management Console -> Organization Configuration -> Mailbox -> Offline Address Book Tab, make sure this exists and the distribution is set to Web-based and Public Folders.
2. Exchange Management Console -> Server Configuration -> Mailbox -> Database Management Tab -> Mailbox Database -> Properties -> Client Settings tab, verify the OAB is text box is filled in, if not click browse and select your OAB.
3. IIS -> OAB Virtual Directory -> SSL Settings -> DO NOT REQUIRE SLL -> Restart IIS
4. Restart Outlook 2007/2003.
Reference: http://forums.microsoft.com/TechNet/ShowPost.aspx?PostID=1398272&SiteID=17
public
class RollingList<T> : ICollection<T> {
#region Private Fields
private int _capacity;
private LinkedList<T> _linkedList;
#endregion #region Constructors
public RollingList(int capacity) {
_capacity = capacity;
_linkedList = new LinkedList<T>();
}
#endregion
#region Public Methods
public override string ToString() {
StringBuilder sb = new StringBuilder();
foreach(T item in this) {
sb.AppendLine(item.ToString());
}
return sb.ToString();
}
#endregion
#region
ICollection<T> Members
public void Add(T item) {
_linkedList.AddFirst(new LinkedListNode<T>(item));
if(_linkedList.Count > _capacity)
_linkedList.RemoveLast();
}
public void Clear() {
_linkedList.Clear();
}
public bool Contains(T item) {
return _linkedList.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex) {
_linkedList.CopyTo(array, arrayIndex);
}
public int Count {
get { return _linkedList.Count; }
}
public bool IsReadOnly {
get { throw new Exception("The method or operation is not implemented."); }
}
public bool Remove(T item) {
return _linkedList.Remove(item);
}
#endregion #region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator() {
return _linkedList.GetEnumerator();
}
#endregion
#region
IEnumerable Members
IEnumerator IEnumerable.GetEnumerator() {
return _linkedList.GetEnumerator();
}
#endregion
}
ManagementObjectSearcher
searcher = new ManagementObjectSearcher("SELECT * FROM Win32_SerialPort");
foreach(ManagementObject obj in searcher.Get()) {
Console.WriteLine(obj["DeviceID"].ToString());
}
There a lot of things to think about when it comes to serializing static fields, how it will be stored, how it will be reinstated, duplication of data, and design. Although, it usually is a sign of bad design if you are trying to serialize static fields, there are some cases in which you need to. In which cases, some work arounds are way to tedious, for what you are actually trying to do, especially some sort of templating. Well here's one way you can do it.
public class Desk {
private static string _manufacturer = "Hard Furniture - {Id}";
private static string _serial = "HFSTYLE{Id}";
private int _id;
// Properties to serialize static fields
public string Manufacturer {
get { return _manufacturer; }
set { _manufacturer = value; }
}
public string Serial {
get { return _serial; }
set { _serial = value; }
}
// Properties
public int Id {
get { return _id; }
set { _id = value;
SetupLocals();
}
}
private
void SetupLocals() {
// Assign local based on static
someLocal = Manufacturer.Replace("{Id}", Id.ToString());
someLocal2 = Serial.Replace("{Id}", Id.ToString());
}
// Constructors
private Stop() { }
public Stop(int id) {
Id = _id;
}
}
Have you ever been developing a Windows Forms application, and then somewhere in the form you want to create a new Timer (System.Threading). When you do this you run into a ambiguous reference problem because System.Windows.Forms already contains definition for a Timer. So, usually you just end up prefixing wherever you want a Threading Timer by adding the "System.Threading" text in front of it. Although, this does work it creates harder to read code, there is a solution.
using
System;
using t = System.Threading;
using tTimer = System.Threading.Timer;
using System.Windows.Forms;
namespace AmbiguousReferences {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
Timer winFormsTimer = new Timer();
t.Timer threadTimer1 = new System.Threading.Timer(ShowMessage, "Timer 1", 500, t.Timeout.Infinite);
tTimer threadTimer2 = new tTimer(ShowMessage, "Timer 2", 500, t.Timeout.Infinite);
}
private void ShowMessage(object msg) {
Invoke((MethodInvoker)delegate() {
MessageBox.Show(msg.ToString());
});
}
}
}
I have seen a lot of code lately using "is" instead of "as". I just want to get the word out there that it is much better to use "as" as it is more efficient. For example, here's a sample code post using "is":
if
(someType is Point) {
Point myPoint = someType as Point;
// Use myPoint as needed
}
This post actually casts the object stored in someType twice, to see if it is indeed a Point. Now, look at the following post using "as" which is more efficient. This only casts the object stored in someType once.
Point
myPoint = someType as Point;
if (myPoint != null) {
// Use myPoint as needed
}
Assigning the Icon of a window in WPF, should seem rather simpllistic and straight forward. You should be able to simply grab an icon, say the question mark from SystemIcons.Question and practically just assign it to the Window's Icon property. Oh, but of course not! The Icon property is implemented as a ImageSource, now you have hit a war battle, trying to figure out how to convert this damn Icon into an ImageSource. Well, here's how you do it...
http://infosysblogs.com/microsoft/2007/04/wpf_assigning_icon_to_image_co.html
As you can see from above this post is about using WCF in Windows Services. This means anything I talk about below will be considered a long running service, as this is the point of Windows Services.
Singleton
Session
Per Call
I've finally figured out a way to install Windows Media Player 11 on Windows Server 2003. This has been the only thing holding me back from running Windows Server 2003 on my developer machine.
http://www.msblog.org/index.php/2006/12/17/install-windows-media-player-11-on-windows-server-2003/
I've been playing guitar for quite a while and have accumulated quite a few downloaded tabs, but with how accessible they are on the web I usually just end up deleting them and downloading them later. Lately, there's been a lot of legality issues with tab sites, so some of them have been pretty flaky, a lot are getting shut down from time to time, and then I'm unable to download the good tabs from them. Well, there is one site in particular that I use all the time, I'm not going to mention their name here, as I don't want their bandwith or servers to get hammered by everyone doing what I'm doing. But I was thinking what if I could make a program that could just go through every tab page, then programmatically click the download button, intercept the download and put it in a directory of my choosing, once done, go on to the next tab.
Well it has been done, here's how it works. First on my main form I have a text box a start button, and a web browser control. In the textbox you enter the id number of the tab you would like to start with. (all tabs in this database have an id number and they are incremental, very handy, and lucky) When you click the start button, we first browse to the main tab page, with something like this:
string url = string.Format("http://[site]/tablature.php?id={0}", id.ToString());
webBrowser1.Navigate(url);
Then I kick off a timer to check every 500 ms, if the webBrowser has completed loading or not, once it has we kill the check timer and then perform the click of a button on the web page to start the download of the tab as so:
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) {
// Kill the timer here
ClickDownloadTab();
}
I then had to do some Watch-Debugging, as they did not explicitly name the "Download Tab" button, using the watch window I was able to browse through all input controls and select which one was correct in my case the 3rd index. In order to click the button, we grab the button as a HtmlInputElement and then call the InvokeMember passing in "click" as the event name, this is case-sensitive, and had me fooled, as I first tried "Click" and "Click()".
private void ClickDownloadTab() {
if (webBrowser1.Document != null && webBrowser1.Document.GetElementsByTagName("input").Count > 3) {
_watchingFileDownloads = true;
webBrowser1.Document.GetElementsByTagName("input")[3].InvokeMember("click");
}
}
Now comes the tricky part intercepting the download, so our WebBrowser control (IE) based doesn't automatically show it's download dialog box and screw everything. Conveniently enough, the WebBrowser control has a FileDownload event that we can attach to, unfortunately this event gets called for any download, each page request, etc. This is why I have some flags I set, so I can ignore the page requests we don't want to download. Then I have a downloading flag, so when this gets called again if we're already downloading the file, it stops the browser from browsing anymore, THIS IS WHAT STOPS THE BROWSER FROM SHOWING THE DOWNLOAD FILE DIALOG, here's how it's done:
private bool _watchingFileDownloads = false;
private bool _downloading = false;
private void webBrowser1_FileDownload(object sender, EventArgs e) {
try {
if (_downloading) webBrowser1.Stop();
if (_watchingFileDownloads) {
if (webBrowser1.Document != null) {
HtmlElementCollection metas = webBrowser1.Document.GetElementsByTagName("meta");
if (metas.Count >= 2 && metas[3].OuterHtml.Contains("URL")) {
_watchingFileDownloads = false;
_downloading = true;
string downloadUrl = metas[3].OuterHtml.Substring(43);
downloadUrl = downloadUrl.Remove(downloadUrl.Length - 2);
string fileName = downloadUrl.Substring(downloadUrl.LastIndexOf('/') + 1);
System.Net.WebClient client = new WebClient();
client.DownloadFile(downloadUrl, @"C:\Powertabs\" + fileName);
_downloading = false;
_curId++;
GetPowertab(_curId);
}
}
}
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
Another thing, I should mention for this particular site, they put the physical URL of the actual Tab file in a meta tag. So, I decided to just parse out the meta tag grab the url and then do a WebClient.DownloadFile(), I then increment my tabId and call GetPowertab again...
Pretty neat considering i've downloaded 4,000 tabs in the last 5/6 hours.
More Posts
Next page »