4

For my project I'd like to display C# source code that I get from an external file. All I want to do is to parse that file and if possible display the code with syntax highlighting.

If also possible I'd like to divide the code I read into the various methods.

Where should I start?

3
  • 4
    possible duplicate of [vb.net/C# code editor with color coding ](stackoverflow.com/questions/1111607/…) Commented Nov 25, 2010 at 17:06
  • 1
    Actually that question is for WinForms controls whereas this is tagged WPF Commented Nov 25, 2010 at 17:19
  • Yes and I don't want to have an editor. I'd like to use it in my application, for example in a TextBox i draw. Commented Nov 25, 2010 at 17:32

3 Answers 3

1

I'd recommend AvalonEdit. It's easy to setup and use. Example

xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"

<avalonEdit:TextEditor Name="textEditor"
                       Loaded="textEditor_Loaded"
                       FontFamily="Consolas"
                       FontSize="10pt"/>

private void textEditor_Loaded(object sender, RoutedEventArgs e)
{
    textEditor.Load(@"C:\MainWindow.xaml.cs");
    textEditor.SyntaxHighlighting =
        HighlightingManager.Instance.GetDefinition("C#");
}

Example Output

alt text

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! This looks great, but from the documentation I read, it is more an editor than just a display?
@Roflcoptr: Yes, it's a pretty big component that can do alot of stuff. If you set IsReadOnly="True" then editing will be disabled and it will just display the content of the file.
Thanks!This looks really nice! I was able todisplay a document. Now I just have to find out, if I can catch the event, if a user clicks on a method.And a way to change the click events into the surface specific events.
0

An alternative way is to launch an external tool or app like viewer. For example, in Windows you can use VIM app to open a cs file in read-only and no modification mode:

"C:\Program Files\Vim\vim72\gvim.exe" -R -M C:\test\MyClass.cs

Here are some codes to launch the tool:

public static int StartViewer(string file)
    {
        string parameters = string.Format("-R -M {0}", file);
        ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
        psi.FileName = "C:\Program Files\Vim\vim72\gvim.exe";
        psi.Arguments = parameters;

        Process p = Process.Start(psi);
        p.WaitForExit();

        return p.ExitCode;
    }

I think VIM has CS syntax colorizer, or you can find some better one. It is free and has great search feature. However, VIM may not be good for none-VIM users. This is just an example. You can use other tools if you like to leverage other app strength.

1 Comment

Thanks, but I want to display the source code in the GUI elements of my applicaiton.
0

Another take on syntax highlighting.

1 Comment

Thanks, but isn't this for websites only?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.