0

I'm connecting to a network share with 'WNetAddConnection2' from c#. This works fine so far. The problem is, this connection should only be used by the apllication it creates. But if I connect to the server via Windows explorer, I also see the folders and files used by this share. I tried Impersonation before I call WNetAddConnection2 but this don't help, still the same problem.

Is there a way to connect to a network share with c#, so that this connection is exclusive for the application that it creates?

best regards Andre

Andy
  • 1
  • 1
    Simple answer: No, you cannot. It is global for the session. It is documented as well in the Remarks section for WNetAddConnection2 – Sir Rufo Dec 25 '17 at 22:52
  • In theory, of course you can connect exclusively from your application. You can just implement an SMB client and ignore all the Windows support. But doing so would be fairly complicated, and certainly beyond the scope of a Stack Overflow question. That said, you seem to possibly have an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). It's not clear what you expect to gain by limiting the connection to the process. You might get better traction with your question by focusing on what led you to think this particular approach is useful/good. – Peter Duniho Dec 25 '17 at 23:05
  • thanks for your reply, I found a bit missleading information about WNetAddConnection2 and so I was not shure if it is impossible or I'm doing wrong... For the Background, I'm Building a synchronize apllication, inbetween owncloud and SVN, but with no need for a server, only a client. It works fine so far, just if you use a NAS and have 2 Users for the shares, one for the sync-app, ohne wich you are using for other access to the nas, than the problem is that when the app is running (actively synching) your login to the nas is swichted... – Andy Dec 26 '17 at 08:11
  • Fairly broad question here. But, some thoughts: according to some comments, the net connection is per-session, so you might be able to impersonate your process under some other account before making the network connection; or, consider using a different protocol (custom, or something like HTTP or FTP) to interact with the server. The Windows network sharing is, I believe, unencrypted so there would be other benefits besides keeping the synchronization logic independent of the logged-in user, if you choose a protocol that supports encryption (sadly, these days, an important benefit). – Peter Duniho Dec 27 '17 at 00:52
  • You might check out this comprehensive Q&A, if you haven't already: https://stackoverflow.com/questions/659013/accessing-a-shared-file-unc-from-a-remote-non-trusted-domain-with-credentials – Peter Duniho Dec 27 '17 at 00:53

0 Answers0