igal k
igal k

Reputation: 1934

Opening a network file using CreateFile fails with FILE_NOT_FOUND

So I'm trying to simulate a distant file opening, which is pointing back to my computer, however i keep failing with error 3 (FILE_NOT_FOUND). I went through the following documentation regarding network usage, but it didn't work either.

hFile1 = ::CreateFile(LR"(\\172.17.12.172\C$\Develop\Code\File.txt)", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (INVALID_HANDLE_VALUE == hFile1)
{
    LOG_ERROR(L"Failed opening file with: " << GetLastError());
    break;
}
  1. The FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE flags are for the GetFileInformationByHandle which is used later on, i compare file paths.
  2. I tried opening \\172.17.12.172\C$\Develop\Code\File.txt using notepad, it worked.

172.17.12.172 is my local ip address.

Upvotes: 1

Views: 901

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

The syntax of your file name is fine. That the error code is FILE_NOT_FOUND rather than some other error means that the directory is found, but no file within that directory can be located.

You should be able to open a file with a path of that form using CreateFile. If you really can open the file with that path using Notepad, then you will be able to do the same using CreateFile, so long as you pass the same file name.

So the most plausible explanation is that you simply made a typographical error. I see no reason to look beyond the obvious conclusion suggested by FILE_NOT_FOUND. There is no file of that name.

Upvotes: 1

Related Questions