How to create NAMED-PIPE in .NET-4 in your C# application?
Here is a piece of code to create a Named Pipe client, it is lifted from an answer to a previous question I answered on communicating between C++ and C# using Named Pipes
using System;
using System.Text;
using System.IO;
using System.IO.Pipes;
namespace CSPipe
{
class Program
{
static void Main(string[] args)
{
NamedPipeClientStream pipe = new NamedPipeClientStream(".", "HyperPipe", PipeDirection.InOut);
pipe.Connect();
using (StreamReader rdr = new StreamReader(pipe, Encoding.Unicode))
{
System.Console.WriteLine(rdr.ReadToEnd());
}
Console.ReadKey();
}
}
}
The easiest way is using WCF:
var binding = new System.ServiceModel.NetNamedPipeBinding(System.ServiceModel.NetNamedPipeSecurityMode.None)
Related
c# File handler error.I can't find it.
use SafeFileHandle filehandle -> error
I tried to copy it, but it didn't work out well.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Win32.SafeHandles;
namespace test
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("검색할 경로를 입력하세요.");
string result1 = Console.ReadLine();
Console.WriteLine("파일 확장자를 입력하세요.");
string result2 = Console.ReadLine();
Filehandle file1 = new Filehandle();
file1.FileCheck(result1,result2);
}
}
You need to install the package in your project before you can use it.
dotnet add package Microsoft.Win32.SafeHandles
I am trying to access the macros inside of an Access database (accdb).
I tried using:
using Microsoft.Office.Interop.Access.Dao;
...
DBEngine dbe = new DBEngine();
Database ac = dbe.OpenDatabase(fileName);
I found a container["Scripts"] that had a document["Macro1"] which is my target. I am struggling to access the contents of the document. I also question if the Microsoft.Office.Interop.Access.Dao is the best reference for what I am trying to achieve.
What is the best way to view the content of the macros and modules?
You can skip the DAO part, it's not needed in this case. Macros are project specific, so in order to get them all, you would need to loop through your projects. In my example, i just have one project.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Access;
namespace Sandbox48
{
public class Program
{
public static void Main(string[] args)
{
Microsoft.Office.Interop.Access.Application oAccess = null;
string savePath = #"C:\macros\";
oAccess = new Microsoft.Office.Interop.Access.Application();
// Open a database in exclusive mode:
oAccess.OpenCurrentDatabase(
#"", //filepath
true //Exclusive
);
var allMacros = oAccess.CurrentProject.AllMacros;
foreach(var macro in allMacros)
{
var fullMacro = (AccessObject)macro;
Console.WriteLine(fullMacro.Name);
oAccess.SaveAsText(AcObjectType.acMacro, fullMacro.FullName, $"{savePath}{ fullMacro.Name}.txt");
}
Console.Read();
}
}
}
I'd like to have a C# console program which uses Format-Table to display objects. Here's a simple C# program:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
namespace PowerShellFormatTableTest
{
class Program
{
static void Main(string[] args)
{
var ps = PowerShell.Create()
.AddCommand("Get-Process")
.AddCommand("Format-Table");
foreach (var result in ps.Invoke())
{
// ...
}
}
}
}
The majority of the result elements are of course FormatEntryData objects.
Is there a way to print the Format-Table formatted output to the console?
The above example is just a trivial example. Normally, I'll be passing arbitrary objects
If you pipe the result from Format-Table to Out-String, then you should get the same output as a string-object. Try this:
var ps = PowerShell.Create()
.AddCommand("Get-Process")
.AddCommand("Format-Table")
.AddCommand("Out-String");
Here's the example updated to demonstrate Frode's suggestion:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
namespace PowerShellFormatTableTest
{
class Program
{
static void Main(string[] args)
{
var ps = PowerShell.Create()
.AddCommand("Get-Process")
.AddCommand("Format-Table")
.AddCommand("Out-String");
Console.WriteLine(ps.Invoke()[0]);
}
}
}
Im extremely noob in C#. Could you please help me?
I get the following errors:
The name 'client' does not exist in the current context
Identifier expected
Expected class, delegate, enum, interface, or struct
Could you please write me the code that could work properly. I really appreciate your help in advance.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using (WebClient client = new WebClient ());
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
client.DownloadFile("http://yoursite.com/page.html", #"C:\localfile.html");
}
}
}
You've got this:
using (WebClient client = new WebClient ());
in the list of using directives when you really meant it to be a using statement in the method:
static void Main(string[] args)
{
using (WebClient client = new WebClient())
{
client.DownloadFile("http://yoursite.com/page.html",
#"C:\localfile.html");
}
}
Basically the using keyword has two different meanings in C#:
using directives import namespaces and allow type aliases. For example:
using System; // Imports the System namespace
using Cons = System.Console; // Creates an alias for the System.Console type
using statements allow a resource to be easily wrapped in a try/finally block to dispose of the resource at the end of the statement:
using (SomeResource resource = new SomeResource(...))
{
// Use the resource here; it will be disposed of automatically at the
// end of the block.
}
I'm new to C# and I'm new to Speech.Recognition.
I searched very long for tutorials but didn't find that much, I'm even not quiet sure whether I included everything correctly.
I downloaded:
SDK
Runtime
Languages
I'm programming local, I have Windows XP, .net framework 3.5.
Now I just want to get started with some simple lines of code, like to say "hello world" or say one or two words as input.
I tried following, and of course it doesn't work :>
error:
"The Typ- or Namespacename "SpeechSynthesizer" couldn't be found (Is a Using-Direktive or a Assemblyverweis missing?)"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;
using System.Speech.Synthesis;
namespace System.Speech.Recognition { }
namespace System.Speech.AudioFormat {}
namespace System.Speech.Recognition.SrgsGrammar{}
namespace System.Speech.Synthesis { }
namespace System.Speech.Synthesis.TtsEngine { }
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SpeechSynthesizer foo = new SpeechSynthesizer();
foo.Speak("Test");
}
}
}
edit:
hello,
i tried you code,but
using SpeechLib;
couldn't be found :>
well now i wrote:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.SpeechLib;
namespace System.SpeechLib { }
namespace System.Speech.Recognition { }
namespace System.Speech.AudioFormat {}
namespace System.Speech.Recognition.SrgsGrammar{}
namespace System.Speech.Synthesis { }
namespace System.Speech.Synthesis.TtsEngine { }
but I get an error with:
numericUpDown1,SpVoice,SpeechVoiceSpeakFlags,textBox1 and Timeout
Project + Add Reference, .NET tab, select "System.Speech".
A project template pre-selects several .NET assemblies. But only common ones, like System.dll, System.Core.dll, etcetera. You have to add the 'unusual' ones yourself.
you can try this:
get Interop.SpeechLib.dll
using SpeechLib;
private void ReadText(string readText)
{
int iCounter = 0;
while (Convert.ToInt32(numericUpDown1.Value) > iCounter)
{
SpVoice spVoice = new SpVoice();
spVoice.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
spVoice.WaitUntilDone(Timeout.Infinite);
iCounter = iCounter + 1;
}
}