Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
I want to write an output from a page into a txt file which resides in a samba share. There are two Scenarios.
1. On development machine.
2. On hosting server.

Now i am only concerned about development machine. In my application i have a page, the output should be written to a txt file in a samba share. it is in network.

I have no access to samba share. we have created a local user with all access to this share. When i try loging into samba share in windows run command and executing my code it works fine.

But if log off from samba share and again execute the code it will throw "Unknown username or bad password error."

Here is my code.

New Update
I logged into samba share with correct credentials in windows run command and then i executed the code with false credentials, Then also it works fine. I am totally confused now.

System.Net.WebResponse response;
String strMSG = string.Empty;
WebRequest myReq = WebRequest.Create("file://inecsamba/hardware/hosts.txt");
myReq.PreAuthenticate = true;
NetworkCredential networkCredential = new NetworkCredential("username", "pwd");
myReq.Credentials = networkCredential;
myReq.Method = "POST";
using (var sw = new StreamWriter(myReq.GetRequestStream(), System.Text.Encoding.ASCII))
sw.Write("tect to be written");
response = myReq.GetResponse();
strMSG = string.Format("{0} {1}", response.ContentLength, response.ContentType);
catch (Exception ex)
strMSG = ex.Message;
Label2.Text = strMSG;

this username and pwd is localaccount username and password.

Please help me in this . I don't know more about network connection.

If needed i can provide much more details.
Updated 12-Oct-11 20:50pm

1 solution

Your code looks fine compared to the msdn example:[^]

Maybe you could also have a look at the links below and try if they get better results.
Network Shares and UNC paths[^][^]

Good luck!
Share this answer
godson Varghese 13-Oct-11 0:06am    
Thanks for the solution. but it doesn't provide much information. I tried impersonation too, still the same issue exist.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900