Avatar of jrsenthilk
jrsenthilk asked on


How do i define INVALID_HANDLE_VALUE in c#..
I am using CreateFile and i am trying to check the return value..

and also how do i check if CreateFile returned a valid handle or not..
Any sample code in C#

Thank you very much...
.NET ProgrammingC#

Avatar of undefined
Last Comment

8/22/2022 - Mon

If you use System.IO.File.Create(fileName) it will return a valid FileStream object.  If you want to create a TextFile, use System.IO.File.CreateText(fileName) which will return a TextWriter.

Ether of these methods will NOT return an invalid file handle.  If anything goes wrong, an exception will be thrown.

See the following samples from Microsoft:
Mike Tomlinson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Mike Tomlinson

Pretty sure he's talking about the CreateFile() API MogalManic...

    Return Value
    If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot.
    If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.


hi Idle mind,
That was just exactly what i was looking for.
Thank you very much.... It was really very helpful..
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy