Skype API System.IndexOutOfRangeException C# - c#

So there's a program i saw, coded in c#. I keep getting errors on it. System.IndexOutOfRangeException is the main one, its happening at "args[0]". This is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Skype4COMUserProfile
{
class Program
{
private static SKYPE4COMLib.Skype skype = new SKYPE4COMLib.Skype();
[STAThread]
static void Main(string[] args)
{
if (!skype.Client.IsRunning)
{
Environment.Exit(1);
}
skype.Client.OpenUserInfoDialog(args[0]);
}
}
}
I will be very grateful if someone could tell me how to fix this. thank you in advance!

Well that will fail if args is empty. Presumably you're meant to start the program by specifying a user name, or something like that.
You could always check for that:
if (args.Length == 0)
{
// Show an error dialog here
return;
}

Related

How to write a C# Program with a custom method that writes user input to text file?

Edit- Based on Steve and Marco's replies I've edited the code. In the lessons the professor has used StreamWriter so I believe he probably wants us to the same. Here is the revised code:
using System;
using System.IO;
namespace Assignment4
{
class Program
{
static void Main(string[] args)
{
FavoriteNumber();
}
static void FavoriteNumber()
{
Console.WriteLine("Please input your favorite number: ");
var result = Console.ReadLine();
using (StreamWriter writer = new StreamWriter("FavoriteNumber.txt"))
{
writer.WriteLine(result);
}
}
}
}
I am taking a Computer Programming Fundamentals class as a GenEd requirement for an unrelated degree, meaning I am very much a novice at this and really was just interested in gaining a very basic understanding of how it all works. That being said, this is probably a very simple question for most users on this forum. The instructions for this assignment are as follows:
"Write a C# program which uses a custom method to accept user input and save the input to a text file."
Can someone please tell me if the code I wrote meets these parameters? I was really lost with this assignment but am hoping that by some miracle I got it right. Thank you in advance for your help!
using System;
using System.IO;
namespace Assignment4
{
class Program
{
static void Main(string[] args)
{
}
static void addNumbers(int x, int y)
{
int result = x+y;
using (StreamWriter writer = new StreamWriter("Assignment4.txt"))
{
writer.WriteLine(result);
}
}
}
}
You need to accept user input somewhere. You can do that with Console.ReadLine(). It waits until the user types in something and then continues.
Also, you do not need a StreamWriter for this. You can just use File.WriteAllText()
The result would look something like this:
static void WriteTextToFile()
{
WriteTextToFile();
}
static void WriteTextToFile()
{
Console.WriteLine("Please enter some value:");
var valueToWrite = Console.ReadLine();
File.WriteAllText("Assignment4.txt", valueToWrite);
Console.WriteLine("Thanks alot. Press a key to close.");
Console.ReadKey();
}

Run Powershellscript in C#

I am trying to run a PowerShell script via C# in a Windows Form.
The problem is that I have two enums, and I can't get them in the code right:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace WindowsFormsApp6
{
static class Program
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
} *here
}
Just so I understand, do I have to add the following under the static void? (at *here):
using (PowerShell PowerShellInstance = PowerShell.Create())
{
}
Then, do I copy paste it in there?
But of course it's not that easy; I Google'd it, but I don't understand what I have to do to get it working...
Enum RandomFood
{#Add Food here:
Pizza
Quesedias
Lasagne
Pasta
Ravioli
}
Enum Meat
{#Add Food here:
Steak
Beaf
Chicken
Cordonbleu
}
function Food {
Clear-Host
$Foods = [Enum]::GetValues([RandomFood]) | Get-Random -Count 6
$Foods += [Enum]::GetValues([Meat]) | Get-Random -Count 1
$foodsOfWeek = $Foods | Get-Random -Count 7
Write-Host `n "Here is you'r List of Meals for this week :D" `n
foreach ($day in [Enum]::GetValues([DayOfWeek])) {
([string]$day).Substring(0, 3) + ': ' + $foodsOfWeek[[DayOfWeek]::$day]
}
}
In the end, I would like to be able to just press on a button on the form, and then have it run the script which outputs it to a textbox.
Is that even possible?
Thanks for any help!
You could place your PowerShell script into a separate file and call it on a bound event.
// When a button is clicked...
private void Button_Click(object sender, EventArgs e)
{
// Create a PS instance...
using (PowerShell instance = PowerShell.Create())
{
// And using information about my script...
var scriptPath = "C:\\myScriptFile.ps1";
var myScript = System.IO.File.ReadAllText(scriptPath);
instance.AddScript(myScript);
instance.AddParameter("param1", "The value for param1, which in this case is a string.");
// Run the script.
var output = instance.Invoke();
// If there are any errors, throw them and stop.
if (instance.Streams.Error.Count > 0)
{
throw new System.Exception($"There was an error running the script: {instance.Streams.Error[0]}");
}
// Parse the output (which is usually a collection of PSObject items).
foreach (var item in output)
{
Console.WriteLine(item.ToString());
}
}
}
In this example, you would probably make better use of the passed in event arguments, and perform some better error handling and output logging, but this should get you down the right path.
Note that running your current script as-is will only declare your Food function, but won't actually run it. Make sure there is a function invocation in your script or C# code.

Dynamically create executing code in C#

So, I have strange question (maybe so stupid), but...
So, my task.
I have same class which gives me same functionality. So, in the main program, which I realize (yes, it's client-server app)) , I want to dynamically create ".exe wrapper" for this class - simplest code like this:
class Program
{
private SameClass mySameClass;
static void Main(string[] args)
{
mySameClass = new mySameClass(args);
Console.Readline();
}
}
In general, I want to create main app which creates slaves in the independent proccesses via dynamically code generation.
So, how to make it and control it?
Thank you.
So SameClass is supposed to contain the same functions and functionality as the process you want to have run multiple times... I guess what you need are Threads.
Not too sure what you mean by dynamically but let's say you have an event that triggers whenever you need a new SameClass process. Just run
SameClass newClone = new SameClass(args);
Trhead _thread = new Thread(new ThreadStart(newClone.Start));
_trhead.Start();
You can probably do this a little more elegant and refactor within SameClass but it's pretty hard to understand your question, so I guess this is the best I can do you hopefully answer your question.
Found solution based on CodeDom. Yes, It doesn't need reflection, sorry.
Code example:
using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Diagnostics;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var compiler = new CSharpCodeProvider();
var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
parameters.CompilerOptions = "/platform:x64";
parameters.GenerateExecutable = true;
CompilerResults results = compiler.CompileAssemblyFromSource(parameters,
#"using System;
class Program {
public static void Main(string[] args) {
Console.WriteLine(""Hello world!"");
Console.ReadLine();
}
}");
var testProcess = new Process();
testProcess.StartInfo.FileName = results.CompiledAssembly.CodeBase;
testProcess.Start();
Console.WriteLine("I've run slave!");
Console.ReadLine();
}
}
}

Error 1 The name 'WriteAt' does not exist in the current context

I'm trying to write a game in C# that runs on my cmd on Windows and I need to be able to write to any part of the box to do that. I found WriteAt used extensively for this purpose, however it doesn't seem to work in VS 2010. I get the error: "The name WriteAt does not exist in the current context"
I have the default:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
at the top of my code. So why can't I use WriteAt?
Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GamePCL
{
class Program
{
static void Main(string[] args)
{
Console.Clear();
for (int x = 0; x < 24; x += 2)
{
WriteAt("█", x, 0);
WriteAt("█", x, 30);
}
}
}
}
When you call a method without an object or type prefix, as in this case WriteAt() (as opposed to for example Console.WriteLine(), which is called on the Console type), the method must exist in the current context, i.e. in the current class.
You copied that code from MSDN without copying the relevant method:
protected static void WriteAt(string s, int x, int y)
{
try
{
Console.SetCursorPosition(origCol+x, origRow+y);
Console.Write(s);
}
catch (ArgumentOutOfRangeException e)
{
Console.Clear();
Console.WriteLine(e.Message);
}
}

Cosmos "Hello world" Plug generate error

I've just installed Cosmos and tried to run the test program given by default. That's the code:
using System;
using System.Collections.Generic;
using System.Text;
using Sys = Cosmos.System;
namespace CosmosKernel2
{
public class Kernel : Sys.Kernel
{
protected override void BeforeRun()
{
Console.WriteLine("Cosmos booted successfully. Type a line of text to get it echoed back.");
}
protected override void Run()
{
Console.Write("Input: ");
var input = Console.ReadLine();
Console.Write("Text typed: ");
Console.WriteLine(input);
}
}
}
When I try to compile it, it says:
Error 8 Plug needed. System.Void System.Threading.Monitor.Exit(System.Object)
at Cosmos.IL2CPU.ILScanner.ScanMethod(MethodBase aMethod, Boolean aIsPlug) in c:\Data\Sources\Cosmos\source2\IL2CPU\Cosmos.IL2CPU\ILScanner.cs:line 663
at Cosmos.IL2CPU.ILScanner.ScanQueue() in c:\Data\Sources\Cosmos\source2\IL2CPU\Cosmos.IL2CPU\ILScanner.cs:line 779
at Cosmos.IL2CPU.ILScanner.Execute(MethodBase aStartMethod) in c:\Data\Sources\Cosmos\source2\IL2CPU\Cosmos.IL2CPU\ILScanner.cs:line 284
at Cosmos.Build.MSBuild.IL2CPUTask.Execute() in c:\Data\Sources\Cosmos\source2\Build\Cosmos.Build.MSBuild\IL2CPUTask.cs:line 239 C:\Program Files (x86)\MSBuild\Cosmos\Cosmos.targets 32 10 CosmosKernel2Boot
I'm using Visual Studio 2010, I have installed all requirements listed here: http://cosmos.codeplex.com/releases/view/123476
Thank you in advance!
"plug needed" error means that you have used some method which relies on an internal call or PInvoke, and thus Cosmos cannot compile it.
You probably use a method that has not been plugged yet or maybe missing a reference to that implementation (which makes Cosmos think it is not implemented)
Use the below guides to help you getting started:
http://www.codeproject.com/Articles/220076/Csharp-Open-Source-Managed-Operating-System-Intro
http://www.codeproject.com/Articles/29523/Cosmos-C-Open-Source-Managed-Operating-System
Update: Try using something similar to this code:
using System;
using Cosmos.Compiler.Builder;
namespace CosmosBoot1
{
class Program
{
#region Cosmos Builder logic
// Most users wont touch this. This will call the Cosmos Build tool
[STAThread]
static void Main(string[] args)
{
BuildUI.Run();
}
#endregion
// Main entry point of the kernel
public static void Init()
{
var xBoot = new Cosmos.Sys.Boot();
xBoot.Execute();
//There's supposed to be a bit of text here. Change it to Console.WriteLine("Hello world!");
}
}
}
It seems that Cosmos isn't working well with windows 8/8.1. So the only solution is to either install Windows 7 or run a virtual machine with installed Windows 7 (the latter worked for me)

Categories