I was trying to display all available printer names on the server and number of trays associated to them. How to display number of trays associated to the printer?
I used the following code. It works but it doesn't display communicate with the all the information. Should I include messageBox? How to display all these information?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Printing;
using System.Drawing.Printing;
using System.Management;
namespace Find
{
class Program
{
static void Main(string[] args)
{
var printerQuery = new ManagementObjectSearcher("SELECT * from Win32_Printer");
foreach (var printer in printerQuery.Get())
{
var name = printer.GetPropertyValue("Name");
var status = printer.GetPropertyValue("Status");
var isDefault = printer.GetPropertyValue("Default");
var isNetworkPrinter = printer.GetPropertyValue("Network");
Console.WriteLine("{0} (Status: {1}, Default: {2}, Network: {3}",
name, status, isDefault, isNetworkPrinter);
}
}
}
}
Your code works fine for me. It's likely that your console window is displaying the information very quickly and then exiting. Try adding Console.ReadKey() at the end of your Main method, so that the Console session remains after displaying the information.
At the end of your Main method, add something like:
Console.Write("\nDone! Press any key to exit...");
Console.ReadKey();
Related
enter image description hererecently,i need to watermark the pictures in our databases,we use C# mvc,so i want to use pythons2 PIL lib.honest,itis a bad idea,after my fit my PIL,_imaging ,a question still stop me.
Is it happend to someone?
enter image description here
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
public static void Main(string[] args)
{
Microsoft.Scripting.Hosting.ScriptRuntime runtimeobj = IronPython.Hosting.Python.CreateRuntime();
dynamic obj = runtimeobj.UseFile("H:\\vs2015setup_pros\\WebApplication3\\WebApplication3\\bin\\te.py");
Console.WriteLine(obj.f1());
Console.ReadKey();
}
}
}
pythoncode
# -*- coding:utf8 -*-
import sys
sys.path.append(r"H:\vs2015setup_pros\ConsoleApplication2\packages\Lib\site-packages")
sys.path.append(r"H:\vs2015setup_pros\ConsoleApplication2\packages\DynamicLanguageRuntime.1.2.2\lib\Lib")
sys.path.append(r"H:\vs2015setup_pros\ConsoleApplication2\packages\Lib")
#sys.path.append(r"H:\vs2015setup_pros\ConsoleApplication2\packages\DynamicLanguageRuntime.1.2.2\lib\net45\Lib")
#sys.path.append(r"H:\vs2015setup_pros\ConsoleApplication2\packages\DynamicLanguageRuntime.1.2.2\lib\Lib\site-packages\Pillow-6.1.0-py2.7-win32.egg")
#
s="asgrgergerasfefefas"
import time
import random
import math
from PIL import Image,ImageDraw
def f1():
print s.split("s")
print "tiememememfff",time.time()
print random.randint(100,200)
a=Image.open(r"C:\Users\Administrator\Desktop\11.png",)
print a.size
print 21313
newe = Image.new("RGB",(300,200),(100,100,100)) #when the code run to here in C#,it blocked
print 213131311313
newe.save(r"C:\Users\Administrator\Desktop\0000.png")
ss = ImageDraw.Image
drawyy = ImageDraw.Draw(a)
print 768767676
drawyy.text((0,1),"fewfwefwfw",fill=(0,25,25))
a.save(r"C:\Users\Administrator\Desktop\777.png")
How to get a user entered inputs in a console application (test app) to a c# program (monitor app)? I have successfully got a string value to my monitor app from the console that is hard corded in the test app. Now I want to allow the user to input a string from the console in test app and I want to capture that user entered string in test app from my monitored app.
Here are the test app and monitor app codes.
test app
using System;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test2");
Console.Write("Enter a string ");
string txtOne = Console.ReadLine();
Console.WriteLine(txtOne);
//Console.ReadLine();
}
}
}
monitor app code
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace GetStandardOutput
{
class Program
{
static void Main()
{
//
// Setup the process with the ProcessStartInfo class.
//
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = #"C:\Users\erandaka\Documents\Visual Studio 2015\Projects\TestApp\TestApp\bin\Debug\TestApp.exe"; // Specify exe name.
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
//
// Start the process.
//
using (Process process = Process.Start(start))
{
//
// Read in all the text from the process with the StreamReader.
//
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
Console.ReadLine();
}
}
}
}
}
Create a function to retrieve the desired value of the c program.
Use DLLImport, to call the function from your C# program:
https://msdn.microsoft.com/en-us/library/aa984739(v=vs.71).aspx
Sorry for the title, I had a hard time trying to summarize this.
I'd like to replace an unknown number of instances of a string with a wrapper. So I'd like to replace this:
Test with two:\t\t tab characters
With this:
Test with two:<span class="" style="white-space:pre">\t\t</span> tab characters
There could be any number of \t characters in the given string, and in multiple locations.
The reason I'm trying to do this is our software uses Chilkat to sent emails, and if HTML content contains tabs, these are not shown at the receiving end. When sent, we use \t to represent a tab, and when viewing the source of the received email, the tabs are there, but outside the source they are not:
Outside Source:
Source:
I tested with GMail, and this wraps the tabs:
I understand this maybe be a Chilkat issue, but I can't find much help on the topic, but if I can get around it as above, I'm willing to try it.
Use this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string input = "Test with two:\t\t tab characters";
string output = Regex.Replace(input, #"(\t+)", "<span class=\"\" style=\"white-space:pre\">$1</span>");
}
}
}
This is what I ended up using. The capture group was picking up each repetition individually, and then the replace was only replacing the first found instance, not the entire capture group
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string input = "Test with two:\t\t tab characters";
string output = Regex.Replace(input, #"(\t)+", "<span class=\"\" style=\"white-space:pre\">$&</span>");
}
}
}
I'm working on a Windows application where I need to use clipboard data. I am trying to copy text from clipboard by the code below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace MultiValuedClipBoard
{
class Class1
{
public String SwapClipboardHtmlText(String replacementHtmlText)
{
String returnHtmlText = "hello";
if (Clipboard.ContainsText(TextDataFormat.Html))
{
returnHtmlText = Clipboard.GetText(TextDataFormat.Html);
Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
}
return returnHtmlText;
}
}
}
Calling the above function by:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace MultiValuedClipBoard
{
class Program
{
static void Main(string[] args)
{
Class1 aas = new Class1();
string a = aas.SwapClipboardHtmlText("chetan");
Console.WriteLine(a);
Console.ReadLine();
}
}
}
When running this code it gives the output "Hello" which is the default value, not clipboard data.
Your code will not work because of two reasons:
[1] When you say:
if (Clipboard.ContainsText(TextDataFormat.Html))
Here you are basically assuming that the clipboard already contains a text and that too in HTML format, but depending on the values you are setting in the clipboard it doesn't look like you are intending to use the pre-existing clipboard value anywhere in your program. So, this if condition should not be there.
[2] Secondly, you are further trying to set the string "chetan" to the clipboard which is definitely not in HTML format. So,
Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
becomes
Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);
Hence, effectively, your new code becomes something like this:
String returnHtmlText = "hello";
//if (Clipboard.ContainsText(TextDataFormat.Html))
//{
returnHtmlText = Clipboard.GetText(TextDataFormat.Text);
Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);
//}
return returnHtmlText;
Clearly Clipboard.ContainsText(TextDataFormat.Html) evaluates to false. Which means that the clipboard in fact does not contain text in the format you specify.
I changed your program to prove the point:
[STAThread]
static void Main(string[] args)
{
Clipboard.SetText("boo yah!", TextDataFormat.Html);
Class1 aas = new Class1();
string a = aas.SwapClipboardHtmlText("chetan");
Console.WriteLine(a);
Console.WriteLine(Clipboard.GetText(TextDataFormat.Html));
Console.ReadLine();
}
Output:
boo yah!
chetan
I am trying to retrieve process information and I'm aware that I can use:
Process[] myProcesses = Process.GetProcesses();
but how do I retrieve the process description? Is it via some Win32 API call? I'm running Vista and when I click under the Processes tab in Task Manager, I see the description.
What you see in Task Manager is actually the Description field of the executable image.
You can use the GetFileVersionInfo() and VerQueryValue() WinAPI calls to access various version informations, e.g. CompanyName or FileDescription.
For .Net way, use the FileDescription member of FileVersionInfo, instantiated with the executable name got via Process.MainModule.FileName.
Another way would be through Assembly. Load the Assembly from the executable image, then query the AssemblyDescriptionAttribute custom attribute.
You just have to go a bit further down the properties.
Suppose you have an instance of notepad running.
Process[] proc = Process.GetProcessesByName("notepad");
Console.WriteLine("Process version- " + proc[0].MainModule.FileVersionInfo.FileVersion);
Console.WriteLine("Process description- " + proc[0].MainModule.FileVersionInfo.FileDescription);
There you go !
This is the only way I could see to do it. I tried Process and Win32_Process, but no go.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
namespace Management
{
class Program
{
static void Main(string[] args)
{
var ps = Process.GetProcesses();
foreach (var p in ps)
{
try
{
var desc = FileVersionInfo.GetVersionInfo(p.MainModule.FileName);
Console.WriteLine(desc.FileDescription);
}
catch
{
Console.WriteLine("Access Denied");
}
}
Console.ReadLine();
}
}
}