C# open file on windows share

P

P4trykx

Hello

I hace a code that opens a file on a windows share.

\\comp1\folder1\file.txt

Folder1 has full access for Everyone. I can open it from explorer on
my computer (my user isn't on set the server). However when I run this
code

FileStream file = new FileStream(serverFilePath, FileMode.Open);

from asp code as the same user as I do it form explorer I get
IOException saying that the user and password are wrong. Howto
convince C# not to login ?
 
A

Andrew Brook

does this work?
System.IO.FileStream fs = new
System.IO.FileStream(serverFilePath,System.IO.FileMode.Open,System.IO.FileAccess.Read);

Andrew
 
K

Kevin Spencer

Hi Patryk,

All processes run in the context of a logged-in user account. If you want to
open a file on another computer, your application must be running in the
context of a user that has permissions to open files on that machine. You
can do this with Impersonation.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
P

P4trykx

does this work?
System.IO.FileStream fs = new
System.IO.FileStream(serverFilePath,System.IO.FileMode.Open,System.IO.FileA­ccess.Read);

no, I get the same Exception
 
P

P4trykx

All processes run in the context of a logged-in user account. If you want to
open a file on another computer, your application must be running in the
context of a user that has permissions to open files on that machine. You
can do this with Impersonation.

Ok, so how does the explorer manage to open this file ?
 
A

Aidy

Explorer is running as the logged on user, your asp.net code is running
under an account local to the machine that has no access rights to any other
machine on the network, so it cannot access files on shares. You can change
the account your anonymous users run as via the iis admin tools and set it
to an account with the appropriate permissions.
 
P

P4trykx

Explorer is running as the logged on user, your asp.net code is running
under an account local to the machine that has no access rights to any other
machine on the network,

But I'm logged as user12 and the same user is in web.config in
impersonate section

<identity impersonate="true" userName="user12" password="somePassowrd"/
There is no users12 on the server(comp1) with the files I want to
access. \\comp1\folder1\file.txt
 
K

Kevin Spencer

Are you logged in to a domain (Active Directory) account?

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
P

P4trykx

Are you logged in to a domain (Active Directory) account?

No, I have a local account.However computer on which I have the
share's is a aomain controller.

I suspect that explorer first tries to accesses the share using logged
user ant if this fails (no such user on the remote computer) it
connects using Guest account.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top