Link to home
Start Free TrialLog in
Avatar of ethar turky
ethar turkyFlag for Saudi Arabia

asked on

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
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

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

Avatar of ethar turky

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial