|
The Platform SDK documentation wouldn't say that (trust me). You should use GetProfileString , which is a macro definition for GetProfileStringA or GetProfileStringW depending on whether or not UNICODE is defined. Practically all Microsoft APIs that take a string as a parameter are defined this way (unless string parameters are explicitly always ANSI or always Unicode).
If you want to set the default printer and don't have to worry about Windows machines (i.e., 9X and Me) or Windows NT 4, then enumerate PrinterSettings.InstalledPrinters , find the printer, and call SetDefaultPrinter defined in winspool.dll. You can more easily use WMI on supported platforms (the platforms that .NET supports, but, IIRC, 98 and NT 4 will need the WMI redist installed) to abstract the differences away if you want to support older Windows platforms. As I wrote some time back in this forum:
using System;
using System.Management;
using System.Reflection;
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyKeyName("dev")]
class Printers
{
static void Main(string[] args)
{
EnumeratePrinters(args.Length > 0 ? args[0] : null);
}
static void EnumeratePrinters(string defaultName)
{
ManagementObject defaultPrinter = null;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
"select * from Win32_Printer");
foreach (ManagementObject printer in searcher.Get())
{
string name = printer["Name"].ToString();
Console.WriteLine("Printer: " + name);
if (defaultName != null &&
string.Compare(name, defaultName, true) == 0)
defaultPrinter = printer;
}
if (defaultPrinter != null)
{
Console.WriteLine("Setting default printer to \"{0}\"",
defaultPrinter["Name"]);
defaultPrinter.InvokeMethod("SetDefaultPrinter", null);
}
}
} WMI is a services that allows scripting and other COM clients to query and modify data on local and remote machines.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Wow. You don't know how much time has been spent by folks trying to figure out ways to set the system default printer. I've scoured the internet, Microsoft KB, etc., and the only references found specify using API calls.
Your example works great. You've used some things I'm not familiar with (e.g., ManagementObject), so I'll have to study this a bit. Thanks, Heath.
|
|
|
|
|
Never mind on that StringBuilder question - I had a senior moment.
|
|
|
|
|
Hi,
I have a Windows Service which uses a NotifyIcon. If the
computer is rebooted, and I log on, I see the icon appearing, everything
OK. If I log off and log on again, the icon is gone. What is happening over
here ? Also, if I manually stop/start the service the Icon appears after
starting the service, but is gone after logging off and on again.
|
|
|
|
|
Without any details or code snippets, it's almost impossible to tell you anything. However, it appears that you're assuming the service is stopped when you log off and started when you log on. That's not the case. Services were created to run before credentials are even required of an interactive user and have nothing to do with whether a user is logged on or off (even if you run the service as an interactive user, a separate window station is created in order to run the service; allowing the service to interact with the desktop interacts with the current interactive user account).
Your evidence also supports that: if you stop and restart the service your icon is back. If you log off and back on, it's not there. When you logged off the service wasn't stopped (it is run by the service control manager (SCM), not the Windows shell).
You should consider creating a separate control panel application that is started from HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run or the [All Users]\Start menu\Programs\Startup folder. This would interact with the service through .NET Remoting (recommended for .NET applications) or some other IPC. You can also send custom commands (see ServiceController.ExecuteCommand and ServiceBase.OnCustomCommand ) to communicate with your service, but you'll limited to a simple int , requiring that you create your own protocol for controlling your service (it is much easier than .NET Remoting or IPC, however, if all you need to do is send simple commands to perform some routine).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for your reply,
I'll try to give some more details :
The service I have isn't stopped when I log off, it keeps running, like it should. I've added a NotifyIcon to the service, and it is displayed only once : Right after your start the service through 'Administrative Tools->Services', or the first time you log in. If you have the time, please try creating a simple service, add a NotifyIcon to it, register as a service and you should see the same.
I only see this with a service withc managed code, a service created with Visual Studio 6.0/ATL code will always show the icon.
|
|
|
|
|
Is there a method to calculate the count of minutes between two given datetime values ?
|
|
|
|
|
Sure. The DateTime and TimeSpan structures have lots of useful helper methods for doing stuff like this.
Here is my (very contrived) example of one way to do it (NOTE: there are several other ways to go about this):
DateTime time1 = DateTime.Now;<br />
DateTime time2 = DateTime.Now.Add(new TimeSpan(0,0,59,0,0));<br />
<br />
TimeSpan span = time2.Subtract(time1);<br />
int minutes = span.Minutes;
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
FYI, the - operator also returns a TimeSpan when subtracting two DateTime , and subtracting a TimeSpan from a DateTime yields another DateTime :
DateTime dt1 = DateTime.Now;
DateTime dt2 = dt1.AddDays(30);
TimeSpan span = dt2 - dt1; Certainly makes for cleaner code, IMO.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Agreed. My post was intentionally explicit. In practice I use the subtraction operator as well.
Thanks for the feedback.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
There is:
DateTime a = new DateTime(2004, 11, 15, 12, 30, 15, 22);
DateTime b = new DateTime(2004, 11, 15, 13, 33, 15, 22);
double minutes = ((TimeSpan) (b - a)).TotalMinutes;
www.troschuetz.de
|
|
|
|
|
Hi,
I developed a User Control (B) that inherits from another User Control (A).
At first, the control worked fine. Thus, I continued development.
Currently, the control (B), that I initially dragged on the form in the test app, still works.
However, when I try to drag an additional instance of the same control to the same form, I get the error "Object reference not set to an instance of an object", showing on the form a picture saying "System.NullReference" with a red icon with a white cross.
When I try to drag the control to a new test project's form, I get this same error.
Still, the original instance I dragged on the form still works. When I step through that control, I can see that in the constructor all values are initialised to the intended values.
The user control (A), that the null-reference user control (B) inherits from, still works fine; I can still drag additional instance of that control (A) to my form without problems.
Both controls do not contain other controls; I just draw things on them and react on mouse actions.
I don't understand why this user control claims to be a null reference, when I drag a new instance on the form, while the initially created instance of the same control, referencing the same source code, still works.
Closing down the developer environment and restarting it, didn't help either.
Has anyone else experienced a problem like this?
Regards,
Jannigje
|
|
|
|
|
I've had cases like this as well. It sounds like there may be an issue with versioning. What happens if you remove the <@Register> tag and references for the control, then re-add them to your containing page?
It may be easier (during development) to add the controls manually by editing the HTML until the control is stable.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
I had tried that, too, several times, without any effect.
After adding a try-catch in my OnPaint, I could run it without problems and it worked at run-time, but it still displayed with an error on my form in design mode.
Then, while, again, checking if all properties had been intialised - without doing any changes (as far as I know), the control suddenly displayed on the form in design mode, as it should.
I've got the feeling that it has something to do with the initialisation of some value. While the value is wrong, it cannot draw anything in design mode (and, apparently, gives a null reference), but at run time, the value is filled in and no problems occur. Still, all values seem to be properly initialised in the constructor.
Strange...
Jannigje
|
|
|
|
|
Are u sure u are not making any reference to the control's parent|container in the contructor?
|
|
|
|
|
when set c#(windows2000) form`s FormBorderStyle=None,and run the application,it show in appbar only with application name ,why no applicaiotn icon?
How can i reset the icon and with FormBorderStyle=None,
or
How to change the appbar item`s icon?
|
|
|
|
|
With FormBorderStyle=None you don't have any header on your form, so neither an application name visible, nor an icon.
Can you clearly specify what problem is?
> when set c#(windows2000) form`s FormBorderStyle=None,and run the
> application,it show in appbar only with application name ,why no
> applicaiotn icon?
Jannigje
|
|
|
|
|
FormBorderStyle=None
run it
at appbar(taskbar) ,the application button one with Text of applecation name.why has not application icon?
|
|
|
|
|
The taskbar displays both the title and the icon set to the form.
Still don't see any problem.
Two icons:
1. You can set an icon to the form itself.
2. You can set an icon to the app in the project settings
(common properties, general).
I tested with a simple app, running a form with FormBorderStyle = None,
in the taskbar the header and icon of the form were shown.
Jannigje
|
|
|
|
|
Dear, Sir and Madam
I bind a TextBox to a Binding.
My problem is how to cancel the text I have edit and return to the previous (value in database) when I press Esc.
Sorry for bad English.
Thank You.
|
|
|
|
|
|
I've bought a Microsft book on using ASP.NET with C#.NET. I've just started coding using ASP>NET. At the top of each page is a menu. I need to include the menu file. In the old ASP I used a include file. Is this still valid in ASP.NET? The reason why I ask is because they didn't mention once about include files. Is this still the same when using ASP.NET?
I usually include a file like this in ASP:
How would I do this in ASP.NET using C#?
Thanks
Brendan Vogt
|
|
|
|
|
Take a look at UserControls. I'm pretty sure your book also covers this issue.
By the way, the ASP.NET would be more appropriate for futher questions regarding this or similar topics.
www.troschuetz.de
|
|
|
|
|
Look at www.sahometraders.co.za. When I mean menu I'm refering to Buying, Renting, Timeshare, Developments, Farms. Would you still use a usercontrol for this?
|
|
|
|
|
Hmm, I think it's a bit difficult in this case.
The menu appears on all pages and the look stays more or less the same. But on the other hand the lower row of your menu contains different links on each page.
So when using a UserControl it would be easy to apply changes to the look of your menu but it would have to be costumizable during runtime (apply different links for each page).
Implementing the menu on each page would be the easier way until you decide to change the look.
So mainly this is a design question and up to you. How did you solve this when using the include file?
www.troschuetz.de
|
|
|
|
|