Click here to Skip to main content
Click here to Skip to main content

What is a PGN file and how to write PGN files

, 27 Sep 2007
Rate this:
Please Sign up or sign in to vote.
What is a PGN file and how to write PGN files
Screenshot - PGNFileExample.gif

Introduction

What is a PGN File?

A PGN file is a file that is worldwide used by most chess programs. The file basically stores the full game between two players.

In this article I am going to explain how to write your own PGN file using .NET

Using the code

A PGN File is basically a text file - that consist of two sections. The first section a description of the specific game - example what tournament the game was played at who was the players and some more info,

The second part is the actual games using standard FIDE notation.

So from a .NET point of view to write the file is fairly simple, because you only have to make a reference to the System.IO namespace and then write the file.

'Start writing the PGN File

Dim sw As New StreamWriter("c:\PGNFileExample.pgn")


'First write the info about the pgn file (game)

'Make sure the info section start with a "[" sign and ends with a "]" sign.



sw.WriteLine("[Event ""TournamentName""]")

sw.WriteLine("[Site ""?""]")

sw.WriteLine("[Date ""2007.09.27""]")

sw.WriteLine("[Round ""1""]")

sw.WriteLine("[White ""Louw, G.""]")

sw.WriteLine("<Black ""Mills, R."">")

sw.WriteLine("[Result ""1-0""]")

sw.WriteLine("[ECO ""A50""]")

sw.WriteLine("[PlyCount ""25""]")


'Now write the actual game moves with standard FIDE notation

sw.WriteLine("1. c4 b6 2. d4 e6 3. a3 Bb7 4. Nc3 Nf6 5. d5 Bd6 6. Nf3 O-O 7. e4 exd5 8. exd5 c6 9. Be2 Na6 10. O-O Nc7 
11. Bg5 Re8 12. Qd2 h6 13. Bh4 cxd5 14. cxd5 a6 15. Rfe1 b5 16. Nd4 b4 17. axb4 
Bxb4 18. Bf3 Rxe1+ 19. Rxe1 Bxd5 20. Nf5 Bxf3 21. gxf3 Ncd5 22. Kh1 Nxc3 23. Rg1 
g6 24. Qxh6 ")

'Lastly write the result

sw.WriteLine("1-0")

'Any chess program that reads PGN files should now be able to import your chess game!

sw.Close()


Points of Interest

Hope this article make it easy for you to understand what is a PGN file.

and how to write PGN files,

If you have any comments or questions you are more then welcome to mail me or post it on the comment section.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Louwgi
Web Developer
South Africa South Africa
Gideon Louw is a qualified MCSD and software professional.
 
He developed several industrial and government related applications that has successfully been installed all over the world.

Comments and Discussions

 
GeneralMy vote of 1 Pinmembercariolihome21-Aug-11 6:17 
GeneralWorthless PinmemberMKauffman27-Sep-07 7:34 
GeneralRe: Worthless PinmemberLouwgi30-Sep-07 22:18 
GeneralRe: Worthless PinmemberMKauffman1-Oct-07 7:30 
GeneralRe: Worthless PinmemberLouwgi1-Oct-07 23:07 
GeneralRe: Worthless PinmemberLouwgi1-Oct-07 23:12 
GeneralRe: Worthless PinmemberLouwgi1-Oct-07 23:14 
GeneralRe: Worthless PinmemberMKauffman2-Oct-07 8:10 
GeneralRe: Worthless PinmemberLouwgi2-Oct-07 22:33 
GeneralRe: Worthless PinmemberEspiritu19-Aug-11 5:22 
GeneralIf you could.... Pinmembergwestwater27-Sep-07 3:30 
GeneralRe: If you could.... PinmemberLouwgi27-Sep-07 3:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 27 Sep 2007
Article Copyright 2007 by Louwgi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid