65.9K
CodeProject is changing. Read more.
Home

Microsoft Reserved Words that Cannot be Used as File Name

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.41/5 (8 votes)

Dec 19, 2015

CPOL
viewsIcon

11538

A tip that you need to know when you want to create many file names

Introduction

I am writing a small tool that needs to create lot of files with different names(9000+). Then I got one strange error message:

System.ArgumentException occurred HResult=-2147024809 Message=FileStream will not open 
Win32 devices such as disk partitions and tape drives. Avoid use of "\\.\" in the path. 
Source=mscorlib StackTrace: at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, 
Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, 
SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, 
Boolean useLongPath, Boolean checkHost) InnerException:  

I did some research and found out the reason. I have listed the results here to share with you. Hope it will save you some time in the future.

Cause

Microsoft has reserved the following words that cannot be used as file names:

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.?

Reference