I'm making a Silverlight app for students and I'm looking for a key component. In the application, the students should be able to add C# code (one class) in a text box. What I would like to do, is to: Validate that the code is valid C# and ideally also make sure it correctly implements a given interface. Is there a control out there which can help me with this?
Chris
Example Code based on the CodecomProvider:
private void button1_Click(object sender, System.EventArgs e)
{
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
string Output = "Out.exe";
Button ButtonObject = (Button)sender;
textBox2.Text = "";
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
//Make sure we generate an EXE, not a DLL
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, textBox1.Text);
if (results.Errors.Count > 0)
{
textBox2.ForeColor = Color.Red;
foreach (CompilerError CompErr in results.Errors)
{
textBox2.Text = textBox2.Text +
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
}
else
{
//Successful Compile
textBox2.ForeColor = Color.Blue;
textBox2.Text = "Success!";
//If we clicked run then launch our EXE
if (ButtonObject.Text == "Run") Process.Start(Output);
}
}
Add the beginning of the file, add these using statements:
using System.CodeDom.Compiler;
using System.Diagnostics;
Just to conclude and answer my own question (thanks to Luke Woodward): This isn't possible as the System.CodeDom.Compiler namespace is just about empty in the Silverlight framework :-(
Related
I have created a tool that open a c# Winform application, the question is after I made some modification to the files I want to recompile those files programmatically using c#.
You can do it by using CSharpCodeProvider. You can find more information about this class in documentation.
after searching i have use this solution :
String[] csFiles = CopyAllSourceFilesToCLASSESFolder().ToArray();
String[] referenceAssemblies = { "System.dll", "System.Drawing.dll", "System.Windows.Forms.dll", "ICSharpCode.TextEditor.dll", "System.Xml.dll", "System.IO.dll", "System.ComponentModel.dll" , "System.Data.dll" , "System.linq.dll" };
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();
string Output = "Out.exe";
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(referenceAssemblies);
//Make sure we generate an EXE, not a DLL
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = icc.CompileAssemblyFromFileBatch(parameters, csFiles);
if (results.Errors.Count > 0)
{
foreach (CompilerError CompErr in results.Errors)
{
ErrorTextBox.Text = ErrorTextBox.Text +
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
}
else
{
//Successful Compile
ErrorTextBox.ForeColor = Color.Blue;
ErrorTextBox.Text = "Success!";
//If we clicked run then launch our EXE
}
Process.Start(Output);
I am trying to do a Visual Studio kind of program, which allows you to type code into a RichTextBox. After pressing F5(Compile), it would compile the code. How would the user compile said code? I know how to use the ConsoleApplication Compiler, but not compiling Windows Forms :(
Could someone help? A code-example is preferable, but at this point, I'll accept ANYTHING!
My current code for Console Apps is this:
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();
string Output = "MCCACOut.exe";
Button ButtonObject = (Button)sender;
richTextBox201.Text = "";
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
//Make sure we generate an EXE, not a DLL
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = icc.CompileAssemblyFromSource(parameters, richTextBox301.Text);
if (results.Errors.Count > 0)
{
richTextBox201.ForeColor = Color.Red;
foreach (CompilerError CompErr in results.Errors)
{
richTextBox201.Text = richTextBox201.Text +
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
}
else
{
//Successful Compile
richTextBox201.ForeColor = Color.Blue;
richTextBox201.Text = "Success!";
//If we clicked run then launch our EXE
Process.Start(Output);
}
Could anyone convert this to compile WinForms instead of ConsoleApp? :)
I think you have to save your file with .cs extension and invoke a process to compile it using c# compiler csc.exe.
After saving the file, you can compile the code using,
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe [options] filename.cs
Take a look at this for options
You can invoke a process to do this from your IDE.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe filename.cs";
process.StartInfo = startInfo;
process.Start();
Hope this helps.
Finally, after many, many times of struggling, I got it to work :) With the help of Nishan Chathuranga
string compiledOutput = "Generated.exe";
//COMPILATION WORK
String[] referenceAssemblies = { "System.dll", "System.Drawing.dll", "System.Windows.Forms.dll" };
CodeDomProvider _CodeCompiler = CodeDomProvider.CreateProvider("CSharp");
System.CodeDom.Compiler.CompilerParameters _CompilerParameters =
new System.CodeDom.Compiler.CompilerParameters(referenceAssemblies, "");
_CompilerParameters.OutputAssembly = compiledOutput;
_CompilerParameters.GenerateExecutable = true;
_CompilerParameters.GenerateInMemory = false;
_CompilerParameters.WarningLevel = 3;
_CompilerParameters.TreatWarningsAsErrors = true;
_CompilerParameters.CompilerOptions = "/optimize /target:winexe";//!! HERE IS THE SOLUTION !!
string _Errors = null;
try
{
// Invoke compilation
CompilerResults _CompilerResults = null;
_CompilerResults = _CodeCompiler.CompileAssemblyFromSource(_CompilerParameters, richTextBox1.Text);
if (_CompilerResults.Errors.Count > 0)
{
// Return compilation errors
_Errors = "";
foreach (System.CodeDom.Compiler.CompilerError CompErr in _CompilerResults.Errors)
{
_Errors += "Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";\r\n\r\n";
}
}
}
catch (Exception _Exception)
{
// Error occurred when trying to compile the code
_Errors = _Exception.Message;
}
//AFTER WORK
if (_Errors == null)
{
// lets run the program
MessageBox.Show(compiledOutput + " Compiled !");
System.Diagnostics.Process.Start(compiledOutput);
}
else
{
MessageBox.Show("Error occurred during compilation : \r\n" + _Errors);
}
This works like a charm!
I have winforms c# project and in that I have two .txt files i.e., credentials.txt and RetailButton_Exception.txt. Now I have given path to D: drive at development side. Now what if I install my application in different pc which does not have D: ?
I have given the code for saving these files as follow:-
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
string Log_API = "http://api.retailbutton.co/WS/Service.php?Service=employeeLogin";
if (LoginUser(Log_API))
{
logIn_Status = "true";
GlolbalUtil.authenticate = "true";
GlolbalUtil.LogIn_Status = logIn_Status;
this.Hide();
//string credentialPath = #"D:\credentials.txt";
String test = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (File.Exists(test + "credentials.txt"))
{
using (StreamWriter writer = new StreamWriter(test, true))
{
//writer.WriteLine("UserName :" + txtUsername.Text + Environment.NewLine + "Password :" + txtPassword.Text);
writer.WriteLine(txtUsername.Text);
writer.WriteLine(txtPassword.Text);
}
frmDash frmDash = new frmDash();
frmDash.Owner = this;
frmDash.Show();
txtUsername.Text = "";
txtPassword.Text = "";
}
else
{
using(FileStream fs = File.Create(test))
{
#region
using (StreamWriter writer = new StreamWriter(test, true))
{
//writer.WriteLine("UserName :" + txtUsername.Text + Environment.NewLine + "Password :" + txtPassword.Text);
writer.WriteLine(txtUsername.Text);
writer.WriteLine(txtPassword.Text);
}
frmDash frmDash = new frmDash();
frmDash.Owner = this;
frmDash.Show();
txtUsername.Text = "";
txtPassword.Text = "";
#endregion
}
#region
//using (StreamWriter writer = new StreamWriter(credentialPath, true))
//{
// //writer.WriteLine("UserName :" + txtUsername.Text + Environment.NewLine + "Password :" + txtPassword.Text);
// writer.WriteLine(txtUsername.Text);
// writer.WriteLine(txtPassword.Text);
//}
//frmDash frmDash = new frmDash();
//frmDash.Owner = this;
//frmDash.Show();
//txtUsername.Text = "";
//txtPassword.Text = "";
#endregion
}
//GlolbalUtil.accept_status = "1";
}
else
{
MessageBox.Show("Please Check Username and password");
}
}
catch (Exception ex)
{
string filePath = #"D:\RetailButton_Exception.txt";
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
"" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
}
}
The answer is simple - you shouldn't use absolute path. Choose one of specific system folders depending on what you're doing. Check folders here I suggest using ApplicationData if it's per user files. Or use CommonApplicationData if it's per system files.
That's how most of the applications behave now. Don't try to store app created files in your installation folder cause it's not per user. It's per machine.
It may be even prohibited for your app to write to Program Files cause it's folder is not intended to store application configuration.
An example from msdn on how to use SpecialFolders.
// Sample for the Environment.GetFolderPath method
using System;
class Sample
{
public static void Main()
{
Console.WriteLine();
Console.WriteLine("GetFolderPath: {0}",
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
}
}
Sample how to store info there:
static void Main(string[] args)
{
var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "userinfo.txt");
using (var writer = new StreamWriter(file))
{
writer.WriteLine("Hello, World!");
}
}
You can also use Application.StartupPath for building you path, it is the path where you executable is placed.
string credentialFilePath = Path.Combine(Application.StartupPath, "credentials.txt");
string retailExceptionFilePath = Path.Combine(Application.StartupPath, "RetailButton_Exception.txt");
You can place your txt files in the same folder with your compiled exe file.
Then, you can use a relative path like this:
File.WriteAllText(#"credentials.txt", String.Empty);
If your application will be installed on:
C:\Program Files\YourApplication\yourapplication.exe
Then it will try to open
C:\Program Files\YourApplication\credentials.txt
Moreover, you can add your txt files to a Visual Studio project and set a property Copy To Output Directory to Copy if newer.
After that your output directory will always have these two files and it will be easier for you to work with it.
Just use your application folder, you can use it by Application.StartupPath
try doing this
string yourfilepath = Application.StartupPath + "credentials.txt";
string secondfile = Application.StartupPath + "RetailButton_Exception.txt";
I am working in c# 4.0, i want to generate an executable file dynamically, so i used Code Dome, but when i executes it open in console and after then my form displays, i want to generate winform executable file. How can i achieve my aim. the code is below :
string Code = #"
using System;
using System.Windows.Forms;
namespace CSBSS
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public class Form1 : Form
{
}
}
";
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
string tempFolder = #"..\DynamicOutput";
string Output = System.IO.Path.Combine(tempFolder, #"CSBSS.exe");
if (!System.IO.Directory.Exists(tempFolder))
{
System.IO.Directory.CreateDirectory(tempFolder);
}
else
{
if (System.IO.File.Exists(Output)) System.IO.File.Delete(Output);
}
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
parameters.TempFiles = new TempFileCollection(tempFolder, false);
//Make sure we generate an exe.
parameters.GenerateExecutable = true;
parameters.GenerateInMemory = false;
parameters.OutputAssembly = Output;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, Code);
string OutputMsg = "";
if (results.Errors.Count > 0)
{
string msgDescr = "";
foreach (CompilerError CompErr in results.Errors)
{
msgDescr += "Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
OutputMsg = #"Error occured while generating executable file, please check following internal error
" + msgDescr;
//return false;
}
else
{
OutputMsg = "Executable file has been generated successfully.";
}
Specify the output type to be a Windows application by using the CompilerOptions:
parameters.CompilerOptions = "/target:winexe";
I am fairly new to C# and I have written several functioning programs, but all of them have been single thread applications. This is my first multi-threaded application and I am struggling to resolve this "Cross-thread operation not valid: Control 'cbLogType' accessed from a thread other than the one it was created on" error. My application searches Windows Event viewer for a user defined Event ID in a user defined Event Log Source(cbLogType). I am using a backgroundworker process to do all the work and I am using the worker.reportprogress to update a label, however, I receive the above error when debugging. I have tried several Invoke methods, but none seem to resolve my error. I have also tried removing the combobox and setting the Log Source directly in the code, which works to an extent, but still fails. I have included my code and any help would be greatly appreciated. I suspect that I might not be using the Invoke method correctly. Thanks in advance!
CODE:
private void bgWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
{
if (File.Exists(#"C:\Events.log"))
MessageBox.Show("File 'Events.log' already exists. All new data will be appended to the log file!", "Warning!");
string message = string.Empty;
string eventID = (tbEventID.Text);
string text;
EventLog eLog = new EventLog();
Invoke((MethodInvoker)delegate() { text = cbLogType.Text; });
eLog.Source = (this.cbLogType.Text); // I am receiving the error here
eLog.MachineName = ".";
int EventID = 0;
string strValue = string.Empty;
strValue = tbEventID.Text.Trim();
//string message = string.Empty;
EventID = Convert.ToInt32(strValue); // Convert string to integer
foreach (EventLogEntry entry in eLog.Entries)
{
int entryCount = 1;
if (cbDateFilter.Checked == true)
{
if (entry.TimeWritten > dtPicker1.Value && entry.TimeWritten < dtPicker2.Value)
if (entry.InstanceId == EventID)
message = "Event entry matching " + (tbEventID.Text) + " was found in " + (cbLogType.Text);
using (StreamWriter writer = new StreamWriter(#"C:\Events.log", true))
writer.WriteLine("EventID: " + entry.InstanceId +
"\r\nDate Created: " + entry.TimeWritten +
"\r\nEntry Type: " + entry.EntryType +
"\r\nMachinename: " + entry.MachineName +
"\r\n" +
"\r\nMessage: " + entry.Message +
"\r\n");
if (entry.InstanceId != EventID)
message = "No event ids matching " + (tbEventID.Text) + " was found in " + (cbLogType.Text);
}
else
{
if (cbDateFilter.Checked == false)
{
if (entry.InstanceId == EventID)
using (StreamWriter writer = new StreamWriter(#"C:\Events.log", true))
writer.WriteLine("EventID: " + entry.InstanceId +
"\r\nDate Created: " + entry.TimeWritten +
"\r\nEntry Type: " + entry.EntryType +
"\r\nMachinename: " + entry.MachineName +
"\r\n" +
"\r\nMessage: " + entry.Message +
"\r\n");
else if (entry.InstanceId != EventID)
message = "No event ids matching " + (tbEventID.Text) + " was found in " + (cbLogType.Text);
}
bgWorker1.ReportProgress((entryCount) * 10, message);
entryCount++;
}
}
}
}
private void bgWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblStat.Text = e.UserState.ToString();
}
You're accessing cbLogType in a non-UI thread.
Change to
eLog.Source = text;