473,471 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

System.UnauthorizedAccessException when doing MoveTo()

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

Aug 2 '07 #1
1 4558
Hi,
Enable Impersation with admin userid and password.
nygiantswin2005 wrote:
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
Aug 2 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Doug Taylor | last post by:
Hi, I originally posted this in dotnet.security, but have moved it here by request: Hi, I am trying to programmatically add a user with read permissions to the DACL of a registry key. ...
4
by: Jane | last post by:
I got the web application to open up the excel. It works fine on my development box. But when i moved it to the production server, I cannot get the Excel spreadsheet to open. I get...
1
by: Hal 9000 | last post by:
In c# we have a function that creates a virtual directory in IIS 6.0 using DirectoryServices API. The code looks like this: // log in to IIS DirectoryEntry rootDir = new...
0
by: Efi | last post by:
Hi, We have a simple 3 tier application which its core application is VC++ 6.0 ATL COM running as a server application in the COM+. An asp pipe is in charge of handling the requests and passes it...
7
by: Peter Afonin | last post by:
Hello, I'm using this code to access a network share from an asp.net page: Dim dir As DirectoryInfo = New DirectoryInfo("\\10.0.0.150\FormLib\") Dim files() As FileInfo = dir.GetFiles("*.eps")...
0
by: masago | last post by:
Hi....how are you ?? they can help me to solve this problem ?? Access to the path = "c:\windows\microsoft.net\framework\v1.1.4322\Temporary ASP.NET = Files\reports\06639073\bbab30a7" is...
4
by: 101 | last post by:
I get a security error when trying to write out to an XMLSchema file "myDs.WriteXmlSchema(strXMLSchemaFile)". I am running XP SP2, No Domain, MS file and print sharing uninstalled. I was able to...
0
by: nime | last post by:
I've got a problem. I cannot debug my app. which one contains WebBrowser control. I found a resolution but it's for an ASP related problem. I couldn't find a correct "user" to give permisson then...
2
by: job | last post by:
In a sharepoint setup using smartpart to load our user controls using enterprise blocks (data) we are getting some strange errors (logged to the event log). We dont get the error all the time. When...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.