how to know whether I can write something to a directory?

I want to call a API function or some method to know if I have the right to write to specified directory on remote server. like "\\pathname\filename",
before I made modification on "filename", I want to check if I have the certain
access right.
I shared a foler on my machine, and then access this folder from local net to write something, can't be done due to I have no permision to do this.
I can use GetFileAttributes() to check if it is read only, but this function return the same result both local or remote environment.

What other functions can I call?
Thanks.
You can mess with the security attributes API, but the simplest way would be simply to try writing a test file.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <algorithm>
#include <string>
#include <windows.h>

bool IsDirectoryWriteable( const std::string& dirname )
  {
  std::ofstream f;
  std::string   filename;
  std::string   pathname = dirname;

  // Make sure the path is of the form "...\foo\bar\baz\"
  if (pathname.find_last_of( "/\\" ) != pathname.length()-1)
    pathname += "\\";
  std::replace( pathname.begin(), pathname.end(), '/', '\\' );

  // Generate a temporary file name
  filename.resize( MAX_PATH +1, '\0' );

  if (GetTempFileName(
      pathname.c_str(),
      _T("tmp"),
      0,
      const_cast<char*>( filename.c_str() )
      )) == 0
    return false;

  filename.resize( lstrlen( filename.c_str() );

  // Endeavor to create a file
  f.open( filename.c_str() );

  if (!f) return false;

  f.close();
  DeleteFile( filename.c_str() );

  return true;
  }


I wrote this, but in Pascal. I have not tested this C++ version. I'm 99% sure it will work, but that doesn't mean I didn't make a mistake.

Enjoy.
Topic archived. No new replies allowed.