System.UnauthorizedAccessException when doing MoveTo()

  • Thread starter nygiantswin2005
  • Start date

N

nygiantswin2005

Hi

I am trying to resolve this bug that I have in this application.
The code is below. It will generate this Exception

System.UnauthorizedAccessException: Access to the path is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.FileInfo.MoveTo(String destFileName)
at ftpprogram_ns.ftpprogram..ctor()

// csc /reference:ChilkatDotNet2.dll /target:exe /optimize+ /nologo
ftpprogram.cs


using System;
using System.Text;
using System.Xml;
using System.Data;
using System.Data.OleDb;
using System.Net;
using System.IO;
using System.Security;
using System.Security.Permissions;
using Chilkat;

namespace ftpprogram_ns
{
public class ftpprogram
{



public ftpprogram()
{
try
{
PermissionSet fullTrust = new
PermissionSet(PermissionState.Unrestricted);
fullTrust.Demand();

// Make a reference to a directory.
DirectoryInfo di = new DirectoryInfo(@"\\servername\sfolder
\");
// DirectoryInfo di = new DirectoryInfo(@"S:\");
// DirectoryInfo di = new DirectoryInfo(@"E:\IF\");

// Get a reference to each file in that directory.
FileInfo[] fiArr = di.GetFiles();

// Display the names of the files.
foreach (FileInfo fri in fiArr)
{

String file = String.Format(@"\\servername\sharename\{0}",
fri.Name);
// String file = String.Format(@"S:\{0}", fri.Name);
// String file = String.Format(@"E:\IF\{0}", fri.Name);
Console.WriteLine(file);
bool uploadSuccessful = uploadFile(file, fri.Name);

// if upload is successfull move the file to archive
folder
if(uploadSuccessful == true)
{
FileInfo fInfo = new FileInfo (file);
String path = String.Format(@"\\servername\Archive
\{0}", fri.Name);
// String path = String.Format(@"L:\{0}", fri.Name);
// String path = String.Format(@"E:\Archive\{0}",
fri.Name);
Console.WriteLine(path);
Console.WriteLine(fInfo.ToString());
fInfo.MoveTo(path);
}

} // end of foreach


} // end of try
catch(Exception e)
{
Console.WriteLine(e);

}
} // Default Constructor




public bool uploadFile(String filepath, String filename)
{
try
{

Ftp2 ftp = new Ftp2();

bool success;

// Any string unlocks the component for the 1st 30-days.
success = ftp.UnlockComponent("Anything for 30-day trial");
if (success != true)
{
Console.WriteLine(ftp.LastErrorText);
return false;
}

ftp.Hostname = "hostname";
ftp.Username = "username";
ftp.Password = "password";
ftp.Passive = true;

// Connect and login to the FTP server.
success = ftp.Connect();
if (success != true)
{
Console.WriteLine(ftp.LastErrorText);
return false;
}

// Upload a file.
string localFilename;
localFilename = filepath;
string remoteFilename;
remoteFilename = filename;

success = ftp.PutFile(localFilename,remoteFilename);
if (success != true)
{
Console.WriteLine(ftp.LastErrorText);
return false;
}

ftp.Disconnect();
Console.WriteLine("File " + filename + " Uploaded!");
return true;
} // end of try
catch(Exception e)
{
Console.WriteLine(e);
return false;

}


} // end of upload()


[STAThread]
public static void Main()
{

ftpprogram start = new ftpprogram();

} // end of Main()
} // end of class
} // end of namespace
 
Ad

Advertisements

K

Karthik D V

Hi,
Enable Impersation with admin userid and password.

Hi

I am trying to resolve this bug that I have in this application.
The code is below. It will generate this Exception

System.UnauthorizedAccessException: Access to the path is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.FileInfo.MoveTo(String destFileName)
at ftpprogram_ns.ftpprogram..ctor()

// csc /reference:ChilkatDotNet2.dll /target:exe /optimize+ /nologo
ftpprogram.cs


using System;
using System.Text;
using System.Xml;
using System.Data;
using System.Data.OleDb;
using System.Net;
using System.IO;
using System.Security;
using System.Security.Permissions;
using Chilkat;

namespace ftpprogram_ns
{
public class ftpprogram
{



public ftpprogram()
{
try
{
PermissionSet fullTrust = new
PermissionSet(PermissionState.Unrestricted);
fullTrust.Demand();

// Make a reference to a directory.
DirectoryInfo di = new DirectoryInfo(@"\\servername\sfolder
\");
// DirectoryInfo di = new DirectoryInfo(@"S:\");
// DirectoryInfo di = new DirectoryInfo(@"E:\IF\");

// Get a reference to each file in that directory.
FileInfo[] fiArr = di.GetFiles();

// Display the names of the files.
foreach (FileInfo fri in fiArr)
{

String file = String.Format(@"\\servername\sharename\{0}",
fri.Name);
// String file = String.Format(@"S:\{0}", fri.Name);
// String file = String.Format(@"E:\IF\{0}", fri.Name);
Console.WriteLine(file);
bool uploadSuccessful = uploadFile(file, fri.Name);

// if upload is successfull move the file to archive
folder
if(uploadSuccessful == true)
{
FileInfo fInfo = new FileInfo (file);
String path = String.Format(@"\\servername\Archive
\{0}", fri.Name);
// String path = String.Format(@"L:\{0}", fri.Name);
// String path = String.Format(@"E:\Archive\{0}",
fri.Name);
Console.WriteLine(path);
Console.WriteLine(fInfo.ToString());
fInfo.MoveTo(path);
}

} // end of foreach


} // end of try
catch(Exception e)
{
Console.WriteLine(e);

}
} // Default Constructor




public bool uploadFile(String filepath, String filename)
{
try
{

Ftp2 ftp = new Ftp2();

bool success;

// Any string unlocks the component for the 1st 30-days.
success = ftp.UnlockComponent("Anything for 30-day trial");
if (success != true)
{
Console.WriteLine(ftp.LastErrorText);
return false;
}

ftp.Hostname = "hostname";
ftp.Username = "username";
ftp.Password = "password";
ftp.Passive = true;

// Connect and login to the FTP server.
success = ftp.Connect();
if (success != true)
{
Console.WriteLine(ftp.LastErrorText);
return false;
}

// Upload a file.
string localFilename;
localFilename = filepath;
string remoteFilename;
remoteFilename = filename;

success = ftp.PutFile(localFilename,remoteFilename);
if (success != true)
{
Console.WriteLine(ftp.LastErrorText);
return false;
}

ftp.Disconnect();
Console.WriteLine("File " + filename + " Uploaded!");
return true;
} // end of try
catch(Exception e)
{
Console.WriteLine(e);
return false;

}


} // end of upload()


[STAThread]
public static void Main()
{

ftpprogram start = new ftpprogram();

} // end of Main()
} // end of class
} // end of namespace
 

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

Top