-6

I want to create a dll of .cs file by code means by programming in c# can any one help me out how to do it means that i have a two class "Class1" and "Class2" and i want to create dll for class1 by programming so how it will be possible for me please help me out to do it.

EDIT:

ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe");

info.Arguments = @" /out:E:\pratik\file option\Class1.dll Class1.cs";
info.UseShellExecute = true;
Process.Start(info); 
Console.ReadLine(); 

I have use this code to create dll it is running but i am not getiing dll on given path

pratik godha
  • 73
  • 1
  • 8
  • 1
    Have you tried anything so far? Read [faq] and [ask] – Soner Gönül Dec 29 '12 at 12:25
  • 2
    Are you trying to write a compiler in C# for C#? –  Dec 29 '12 at 12:26
  • @H2CO3 That's how it should be done, isn't it? – rene Dec 29 '12 at 12:32
  • please, as others already said, point what you tried, what you want, and what's wrong with the result you've got so far. – Rubens Dec 29 '12 at 12:47
  • @H2CO3 No i just want to make a DLL for individual Classes suppose i have a two class in my application and i want to make a dll for the first Class by CMD it is possible but by code in C# how it will be possible for me ? – pratik godha Dec 29 '12 at 13:19
  • change `info.Arguments = @" /out:E:\pratik\file option\Class1.dll Class1.cs";` to `info.Arguments = @" /out:E:\pratik\fileoption\Class1.dll Class1.cs";` – Tilak Dec 29 '12 at 13:40

2 Answers2

1

You can use compiler as services - CodeDomCompiler feature to create dll/exe on the fly.

How to programmatically compile code using C# compiler

Compiling wiht CodeDom - Article on codeproject

Alternative approach is to compile files CSC.exe command line tool to create the library. For this you need to launch new process with appropriate arguments.

 Process.Start( Path.Combine(GetCscFolderLocation() ,"csc"),  "/target:library File1.cs File2.cs /reference: <reference 1> <reference2> ..."

 string GetCscFolderLocation()
 {
 // Getting CSC location
 }

Getting CSC.exe folder location is tricky. Follow this to get an idea.

Following example starts text file in default editor.

Process.Start(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.e‌​xe", @"/target:library /out:C:\test\test.dll c:\test\File.cs");
Community
  • 1
  • 1
Tilak
  • 30,108
  • 19
  • 83
  • 131
  • "http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx" This Link is not usable for me i just want to create DLL for individual Classes. – pratik godha Dec 29 '12 at 13:13
  • You can just use csc.exe tool in that case. I will update the answer for this approach. – Tilak Dec 29 '12 at 13:18
  • i have already use this link or this code but this is creating only .exe not .dll – pratik godha Dec 29 '12 at 13:27
  • ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"); info.Arguments = @" /out:E:\pratik\file option\Class1.dll Class1.cs"; info.UseShellExecute = true; Process.Start(info); Console.ReadLine(); I have use this code to create dll it is running but i am not getiing dll on given path – pratik godha Dec 29 '12 at 13:34
  • Are you able to compile the files from command line? Also select directory path with spaces. (fileoption instead of file option) If yes, call Process.WaitForExit and check Process.ExitCode – Tilak Dec 29 '12 at 13:37
  • i am getting a error after given this one : C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:library /out:myfile.dll Class2.cs ----"the file could not find the path"---- – pratik godha Dec 29 '12 at 13:58
  • First try to run in command prompt. If this error is coming there, it means path is not correct (Class2.cs does not exist in current directory, or path of csc.exe is wrong) – Tilak Dec 29 '12 at 14:01
  • no when i will use only that path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe " compiler will run but i have to get output of the dll means that location to get dll so i have given ---"/target:library /out:myfile.dll Class2.cs"--- – pratik godha Dec 29 '12 at 14:04
  • try /out:c:\myfile.dll c:\Class2.cs, and copy Class2.cs to c:\ – Tilak Dec 29 '12 at 14:07
  • i already tried this one,but no success – pratik godha Dec 29 '12 at 14:22
  • I have tried following, and it has created file successfuly.`Process.Start(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe", @"/target:library /out:C:\test\test.dll c:\test\File.cs");` . Seems like issue in your file or may be Path is too long (more than 255) – Tilak Dec 29 '12 at 14:26
  • It seems like it working.I assign the parameter in **ProcessStartInfo** object and then call Process.Start(**object**) and its done. – pratik godha Dec 29 '12 at 14:40
  • But i accept you,as you help me to figure out the problem,Thanks – pratik godha Dec 29 '12 at 14:43
  • I have updated the answer with final command. – Tilak Dec 29 '12 at 14:43
0

go to new project in visual studio

select class library name it as you want

now, put method, properties in that class what ever you need

build it.

Add this reference to your project.

Sky5005e
  • 79
  • 5