Click here to Skip to main content
13,558,244 members
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 26 Sep 2007

What is a PGN file and how to write PGN files

, 26 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


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


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


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.


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


About the Author

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.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
cariolihome21-Aug-11 5:17
membercariolihome21-Aug-11 5:17 
GeneralWorthless Pin
MKauffman27-Sep-07 6:34
memberMKauffman27-Sep-07 6:34 
GeneralRe: Worthless Pin
Louwgi30-Sep-07 21:18
memberLouwgi30-Sep-07 21:18 
GeneralRe: Worthless Pin
MKauffman1-Oct-07 6:30
memberMKauffman1-Oct-07 6:30 
GeneralRe: Worthless Pin
Louwgi1-Oct-07 22:07
memberLouwgi1-Oct-07 22:07 
GeneralRe: Worthless Pin
Louwgi1-Oct-07 22:12
memberLouwgi1-Oct-07 22:12 
GeneralRe: Worthless Pin
Louwgi1-Oct-07 22:14
memberLouwgi1-Oct-07 22:14 
GeneralRe: Worthless Pin
MKauffman2-Oct-07 7:10
memberMKauffman2-Oct-07 7:10 
GeneralRe: Worthless Pin
Louwgi2-Oct-07 21:33
memberLouwgi2-Oct-07 21:33 
GeneralRe: Worthless Pin
Espiritu19-Aug-11 4:22
memberEspiritu19-Aug-11 4:22 
GeneralIf you could.... Pin
gwestwater27-Sep-07 2:30
membergwestwater27-Sep-07 2:30 
GeneralRe: If you could.... Pin
Louwgi27-Sep-07 2:40
memberLouwgi27-Sep-07 2:40 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 27 Sep 2007
Article Copyright 2007 by Louwgi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid