save file to network path + file encoding

Dear all,
need to correct this code  , to save file to network path :
				var permissionSet = new PermissionSet(PermissionState.None);
				var writePermission = new FileIOPermission(FileIOPermissionAccess.Write , string.Format("{0}/{1}", sPath, FileName));
				permissionSet.AddPermission(writePermission);

				if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
				{
					using (FileStream fstream = new FileStream(String.Format("{0}/{1}", sPath, FileName) ,   FileMode.Create  ))
					using (StreamWriter writer = new StreamWriter(fstream   ))
					{
						// try catch block for write permissions 					    
							
							writer.WriteLine(contents);
						

					}
				}
				else
				{
					//perform some recovery action here
				 }

Open in new window



when I run this code I got error :
Access to the path '\\xxxx\xxxxx\yyyyy\text.txt' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) in :line 319


Also how to set file encoding.
Thanks
ethar1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
Hi Ethar, I tried a sample by copying your code and it seems to work fine. Please see below.
Are you sure you have correct access permissions set up on network to the drive you are trying to write to? I mean is the path '\\xxxx\xxxxx\yyyyy\" correct? Can you open windows explorer and type this path and see if you are able to open it without any access errors? Also if it open up, can you right click in the windows explorer and try creating a text file with some contents in it and save it? Does that work manually for you?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Security.Permissions;
using System.Text;

namespace FileNetworkPath
{
    class Program
    {
        static void Main(string[] args)
        {
            SaveFile(@"\\karrtikZ5170\g$\karrtik\source\EE\TotalEESolution\TotalEESolution\FileNetworkPath","TestFileEthar.txt","Hi, How are you? I am doing fine.");
        }

        static void SaveFile(string sPath, string sFileName, string contents)
        {
            var permissionSet = new PermissionSet(PermissionState.None);
            var writePermission = new FileIOPermission(FileIOPermissionAccess.Write, string.Format("{0}/{1}", sPath, sFileName));
            permissionSet.AddPermission(writePermission);

            if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
            {
                using (FileStream fstream = new FileStream(String.Format("{0}/{1}", sPath, sFileName), FileMode.Create))
                using (StreamWriter writer = new StreamWriter(fstream))
                {
                    // try catch block for write permissions 					    

                    writer.WriteLine(contents);


                }
            }
            else
            {
                //perform some recovery action here
            }

        }
    }
}

Open in new window

ethar1Author Commented:
yes it work manually perfect .

the application hosted on IIS and try to access a shard folder.
I also map the shared folder to a drive put still face same problem
Karrtik IyerSoftware ArchitectCommented:
It seems to be a security or a configuration of user issue in IIS.
Have seen issues when we do not run as a user with network privileges.
The standard IIS user does not seem to have network privileges.
Please read the article below.
http://imar.spaanjaars.com/276/how-do-i-setup-iis-to-allow-it-to-use-network-resources

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.