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
Related
I'm trying to title case some text that may contain html escape characters. Is there any way of doing this other than with regular expressions? Here's some example code:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string input = "B&G fried pie";
string output = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(input.ToLowerInvariant());
Console.WriteLine(output); //Should be B&G Fried Pie
Console.ReadKey();
}
}
}
Another way I can think of is to replace & with &, do my title case, then replace the & with &.
You can use the System.Web.HttpUtility class to decode and encode html strings, so your code would then look something like:
private static string ToTitleCase(string input)
{
return input == null
? null
: HttpUtility.HtmlEncode(CultureInfo.InvariantCulture.TextInfo
.ToTitleCase(HttpUtility.HtmlDecode(input.ToLowerInvariant())));
}
And in use it would look something like:
Console.WriteLine(ToTitleCase("B&G fried pie"));
So I want to try to read a value from memory using a pointer with 1 offset.
These are my value:
I tried to write some code but it does not work. It only ever reads value 0 but I see on cheat engine the value i is different.
Below is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Trigger
{
class Program
{
static void Main(string[] args)
{
VAMemory vam = new VAMemory("test");
int LocalPlayer = vam.ReadInt32((IntPtr)0x01608310);
while (true) {
int address = LocalPlayer + 0x360;
Console.Write(vam.ReadInt32((IntPtr)address));
//Thread.sleep(500);
}
}
}
}
So can someone help me to understand?
I am trying to load Google and get the ID of the searchbox. The ID of the box is "lst-ib". Which when the program goes to debug it is expecting a semicolon.
Is there a way around it to get the element id? So far I have:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
public void Main(string[] args)
{
Process.Start("www.google.com");
HtmlElement lst-ib = WebBrowser1.Document.All["foo"];
//expects a semi colon on the line above after the element id
if (lst-ib != null)
{
lst-ib.InnerText = "test";
}
Console.ReadKey();
}
}
}
That is C# code and - is not valid in identifiers. Feel free to name the variable as you wish – it has no bearing on what the ID of the element is.
The - is an operator, you cannot use this way!
Here you will find more information about operators:
https://msdn.microsoft.com/en-us/library/6a71f45d.aspx
I recomend you rename - (trace) to _ (underline) or anyway you want
=D
My C# console program copies 2 folders into 1. It works but I am new to C#. I cant figure out how I make the program skip the alert window "are you sure you want to overwrite the files".
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using System.Diagnostics;
namespace MirrorSystem {
class Program {
static void Main(string[] args)
{
string source1 = #"folder1";
string source2 = #"folder2";
string destination = #"destination";
try
{
Console.WriteLine("Starting..");
FileSystem.CopyDirectory(source1, destination, UIOption.AllDialogs);
FileSystem.CopyDirectory(source2, destination, UIOption.AllDialogs);
Console.WriteLine("Success!");
System.Threading.Thread.Sleep(5000);
Environment.Exit(0);
Console.ReadKey();
}
catch (OperationCanceledException)
{
Console.WriteLine("Canceled!");
Console.ReadKey();
}
}
}
}
You can pass the value true as the third (overwrite) parameter. Here is the official documentation for the method:
public static void CopyDirectory(
string sourceDirectoryName,
string destinationDirectoryName,
bool overwrite
)
The description of the overwrite parameter:
overwrite
Type: System.Boolean
True to overwrite existing files;
otherwise False. Default is False.
Source.
You can use method File.Copy --> https://msdn.microsoft.com/es-es/library/9706cfs5(v=vs.110).aspx
The third parameter allows you to specify whether you should or should not overwrite existing files with the same name in the destination folder.
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]);
}
}
}