|
There's always Process.CloseMainWindow . The idea is to avoid P/Invoking when possible since it can help lead to more portable code. It was an example and I would hope that for most code samples I post they just don't take them as-is - the ol' copy and paste. I try to teach people, not hand them the answers so they can continue their pursuit in ignorance. You may a good point, though.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Yes, and I believe that TaskMan.exe is a well-formed Windows app.
Process.CloseMainWindow (MSDN) "in a well-formed application, closes child windows and revokes all running message loops for the application. The request to exit the process by calling CloseMainWindow does not force the application to quit...The behavior of CloseMainWindow is identical to that of a user closing an application's main window using the system menu. Therefore, the request to exit the process by closing the main window does not force the application to quit immediately."
Ian Mariano - Bliki | Blog
"We are all wave equations in the information matrix of the universe" - me
|
|
|
|
|
Can somebody tell me how to Unregister a Service Process so that its name is not displayed in the TaskManager Process List. I required the solution for Windows XP platform. For Win9x platform, we can do it with RegisterServiceProcess API supported by Kernel32.dll. But the same thing is not working with WindowsXP since kernel32.dll doesnot support this API or It does not have this entry point.
Is there some other way to implement this.
CHEERS
I.T. D.U.D.E
|
|
|
|
|
|
Hi
Im developing a program that needs to send emails through outlook because of varius reasons.
This is no problem at first glance using this example: http://support.microsoft.com/default.aspx?id=310263 .. like it says on the page its only for
outlook 2002 and 2003 cause of the mailitem object.
my question then .. how can i make the code work with all outlookz?
regards MTP
|
|
|
|
|
First of all, do you need to actually submit it, or just create a new mail message? If it's the latter case, then just use something like this:
Proces.Start("mailto:user@domain.com?Subject=test"); This will create a new message with any initial subject or body message you specify using the default MAPI client.
If you want a tried-and-true way, just communicate with the SMTP server yourself (the .NET Framework 2.0 will have much better support for this, including support for multi-part MIME messages). There is code on the 'net to get the MX record for a mail host (I've got a solution floating around somewhere here on CodeProject) and use the simple SMTP commands and a TcpClient to send mail to the SMTP server. There are plenty examples of this on the 'net as well. Just google.
You'll have a hard time finding a solution that works with all Outlook versions because they have changed a lot from version to version, patching security holes with terrible solutions. The best way - though pretty inflexible - is through MAPI. There are articles here on CodeProject that cover this. Not everything is available using MAPI, but enough to create and send an email (not browse the address book, however, which is only available to C/C++ clients - or a .NET assembly that P/Invokes the functionality required).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi ..
thanks for the anwser but.
yeah ive tryed Proces.Start thing but i really need my program to fix up the mail for the user with an attatchment which cant be done with the mailto command :'( or atleas i cant make my outlook accept the input i am sending it (mailto:bla@bla.dk?attachment=file:///c:/bla.bla (and alot of other syntaxes)).
Ive already made my own emailsender with mailmessage but then the user wont see the sent / deletet items in outlook which id like em to do.
Is there a way to just open outlook and put an email into one of the folders .. like the sent items? or getting the mailto command to accept attachments?
regards
MTP
|
|
|
|
|
If you're going to try to just put an email into a folder, you might as well use Outlook to send as well.
My suggestion is to just make that a system requirement: Outlook 2002 or above. Or, as I said, search this site for more information on MAPI. You can google, too.
The thing is that MAPI is pretty basic functionality and Microsoft has limited what MAPI can do (like enumerate the address book). They created Extended MAPI (EMAPI) to replace that functionality, but EMAPI does not expose any automation interfaces so that only early-binding languages can use it (C/C++ mainly) - so that script can't access it (and a COM interop library can't be created from it). You can encapsulate EMAPI using .NET, however, by P/Invoking the require methods and creating the IUnknown interfaces yourself. Several people have already done this, so you can use their implementation.
You could also check out Simple MAPI.NET[^] here on CodeProject. This implementation uses MAPI so there will be a security warning for Outlook XP and 2003 that tells the user a program is trying to access certain features, which they can allow or deny.
Encapsulating EMAPI is your best bet, and should work just fine for Outlook 2000 and newer. Anything older would probably be rare, running on a machine that can't run the .NET Framework anyway (i.e., Windows 95), or become too big of a burden and too costly to develop.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi
Thanks very much for the info.
I'll try to give emapi a shot then.
I have tryed the simple mapi example here on the site and it works like a charm. But the problems keeps coming .. the simple mapi example dosnt support the option to add headers to the email like "Disposition-Notification-To". but ill have to settel then.
But once again thanks for the help
MTP
|
|
|
|
|
Hi!
Is it possible to get the name of a variable as a string?
For the following example I want to get the string "variable".
<br />
class Test<br />
{<br />
private int variable;<br />
}<br />
I looked into Reflection namespace, but found nothing that fits well. The namespace provides class FieldInfo but unfortunately the only way to get an instance are the methods GetField or the GetFields . Both methods would more or less require that i already know the name of the variable.
THX in advance!
|
|
|
|
|
I believe there is no way...
why do you need this ?
|
|
|
|
|
I'm storing some data of my class in a XML file that I read out later.
<br />
XmlTextWriter writer = new XmlTextWriter(this.configFilePath, this.configFileEncoding);<br />
writer.WriteStartDocument();<br />
writer.WriteStartElement("Configuration");<br />
writer.WriteElementString("Duration", this.duration.ToString());<br />
writer.WriteEndElement();
writer.WriteEndDocument();<br />
Thought it would be nice to get the string for the localName param by determining the name of the variable instead of hardcoding it.
|
|
|
|
|
You can get the name. Let's say you have a class Test as follows,
class Test<br />
{<br />
private int variable;<br />
private int Variable<br />
{<br />
get{return variable;}<br />
}<br />
private int VariableFunc()<br />
{<br />
return variable;<br />
}<br />
}
Let says we use the FindNames class to get the names of the members of this class. Since we already know the class name we can do,
using System.Reflection;<br />
class FindNames<br />
{<br />
<br />
public string[] GetNamesType()<br />
{<br />
Type mytype = typeof(Test);<br />
ArrayList names = new ArrayList();<br />
PropertyInfo [] properties = mytype.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);<br />
foreach(PropertyInfo pinfo in properties)<br />
{<br />
names.Add(pinfo.Name);
}<br />
FieldInfo [] fields= mytype.GetFields(BindingFlags.NonPublic|BindingFlags.Instance);<br />
foreach(FieldInfo finfo in fields)<br />
{<br />
names.Add(finfo.Name);
}<br />
MethodInfo [] methods = mytype.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);<br />
foreach(MethodInfo methinfo in properties)<br />
{<br />
names.Add(methinfo.Name);
}<br />
MemberInfo [] members = mytype.GetMembers<br />
(BindingFlags.NonPublic|BindingFlags.Instance);<br />
foreach(MemberInfo minfo in members)<br />
{<br />
names.Add(pinfo.Name);
}<br />
<br />
return names.ToArray(typeof(string)) as string[];<br />
}<br />
Make sure you put in the BindingFlags.Instance or BindingFlags.Static or it will throw an exception. Play around with the BindingFlags depending on what type of members you want to get. You can find out many useful things about the member of the class including its type, name, etc. You can also dynamically get/set/invoke it if it is static, or get/set/invoke it on an instance that you pass in. P.S. if you don't want to hardcode the class name too then you can just take an instance of that class and do something like
Type mytype=instance.GetType();
.
.
.
Hope that helped.
|
|
|
|
|
THX for your posting.
I knew I could make it this way and searched for an easier way. Furthermore, what do you do if the class has more than one field? Which element of the names ArrayList do you use?
<br />
FieldInfo [] fields= mytype.GetFields(BindingFlags.NonPublic|BindingFlags.Instance);<br />
foreach(FieldInfo finfo in fields)<br />
{<br />
names.Add(finfo.Name); <br />
}<br />
|
|
|
|
|
Why don't you look into XML serialization? Read the documentatio for the classes in the System.Xml.Serialization namespace. The XmlSerializer already supports this and you can always override defauls using the attributes also in that namespace. Why do what's been already done for you in the .NET FCL?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
THX for pointing me to this. I'm consistently astonished what else the .NET FCL has to offer.
But I think it doesn't fit right for what I'm trying to do as I also want to write comments and private fields to the XML file.
Anyway, I will keep the XmlSerializer in mind. Surely, I will need it sometimes.
|
|
|
|
|
To learn what the FCL has - browse through the class library documentation. You don't have to memorize everything, but at least understand what's there and were. As you develop .NET applications and libraries, you should also see a pattern that Microsoft uses to try to maintain consistency. Like for provider classes: you'll typically see a static Create or CreateInstance method.
If you want to serialize your class in such a way, then you'll have to do it yourself. But take a lesson from both XML and runtime serialization. A good solution would be to create a new attribute - say XmlCommentAttribute - that stores the comment for a field:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class XmlCommentAttribute : System.Attribute
{
private string comment;
public XmlCommentAttribute(string comment)
{
this.comment = comment;
}
public string Comment
{
get { return this.comment; }
}
} This would allow you to declaritivly define the comments for your fields:
[XmlRoot("myClass", Namespace="urn:myClass"]
public class MyClass
{
[XmlComment("This is parsed as a string.")]private string field1;
[XmlElement("myField2"), XmlComment("This is an int.")] private int field2;
} Noticed how I used the XML serialization attributes in places? This allows you to easily rename members or declare how things are stored, whether they should be elements or attributes, etc. If they aren't, you can have your serialization routine default to one or the other. Then just do something like this:
FieldInfo[] fields = myClass1.GetType().GetFields(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (FieldInfo field in fields)
{
if (!field.IsNotSerialized)
{
string name = field.Name;
bool useElement = true;
string comment = null;
XmlElementAttribute[] elemAttrs = (XmlElementAttribute[])
field.GetCustomAttributes(typeof(XmlElementAttribute))
if (elemAttrs != null && elemAttrs.Length > 0)
name = elemAttrs[0].ElementName;
else
{
XmlAttributeAttribute[] attrAttrs = (XmlAttributeAttribute[])
field.GetCustomAttributes(typeof(XmlAttributeAttribute));
if (attrAttrs != null && attrAttrs.Length > 0)
{
useElement = false;
name = attrAttrs[0].AttributeName;
}
}
XmlCommentAttribute[] comments = (XmlCommentAttributes[])
field.GetCustomAttributes(typeof(XmlAttributeAttributes));
if (comments != null && comments.Length > 0)
comment = comments[0].Comment;
}
} Basically, try to handle the basic XML elements so that you can easily override how fields are named. If you ever change private member names (not typically a good idea - always keep serialization in the back of your head when developing) you can use XML attributes to keep their serialized form the same - thus not breaking your new code against old serialized formats.
Finally, use FieldInfo to get the value. This is very easy to use. Do not just use ToString on the returned object (if it's not null). First, try to get the TypeConverter for the Type like so:
TypeConverter converter = TypeDescriptor.GetConverter(field.FieldType); Use that to convert to and from strings. If you just use ToString , you'll have to handle the parsing yourself. Some types provide their own parsing, but typically only the primatives and the methods often use different signatures. You can also use the ConvertTo class, but those only cover the primatives. Using a TypeConverter makes your code more robust.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
What steps do I need to follow for adding a new property to an existing class?
Thanks for your help.
Best regards,
Cristina
|
|
|
|
|
You would have to write your own class, inheriting from the class you want to extend. Then you just supply your property code and the code that will use that property.
RageInTheMachine9532
|
|
|
|
|
I'm a C# newbie. How can I insert a new line into a multi-line TextBox field? I've tried '\n' but instead of inserting a new line, it displays a strange character. Also, I want a particular method to execute when a user presses 'Enter' key after typing something in TextBox. Please help!
mughalali
|
|
|
|
|
|
if the other suggestion doesn't work, it may be something like or one of those sets. for the return, why not do a keypress event?
surgeproof
-------------------------------------------------------
ithium is the best.
'Science without religion is lame, religion without science is blind.' --Albert Einstein
'The pioneers of a warless world are the youth who refuse military service.' --Albert Einstein
|
|
|
|
|
Use Environment.NewLine, which will guarantee the placement of a newline.
|
|
|
|
|
Not necessarily in the TextBox (which encapsulates the Edit common control). To dislay new lines in a TextBox , the Multiline (inheritted from TextBoxBase ) needs to be set to true , as well as the AcceptsReturn property.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
how can i got the version infomation about the sqlsever and any similar information?
should i use the WMI? but which class should i used in the win32_InstalledApplication?
|
|
|
|