save file to network path + file encoding

ethar1
ethar1 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Karrtik IyerSoftware Architect

Commented:
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

Author

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
Software Architect
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial