I would like to know how I can move a file over a network using C#?
I tried this : -
case "218":
-
Console.WriteLine("Backing up " + filename.ToString() + "....");
-
fileinfo.CopyTo("\\\\118.9.11.11" + backUpLocation);
-
break;
-
But I keep getting a S ystem.UnauthorizedAccessException: Access to the path is denied. execption
Please Help
6 1099
Do you have the proper permissions to that location?
Yes I do have all the required permissions on both locations.I can read from from one location but I cant seem to write to a different folder(shared) on the networked computer.
You might have the correct access, but its quite apparent that your program does NOT have the correct access.
How do I correct this?
That's a good question. You're the second person to ask this type of question recently. While I vaguely recall something about how to do it long ago, I cannot seem to find anything about it now.
I guess for starters, if this is a web application, you could use the identity impersonate thing in the web.config. For a windows application...I am not sure. Maybe something in the machine.config file?
I had the same problem, and this is what I found.
It's a bit of a struggle, but it works - using System;
-
using System.Runtime.InteropServices;
-
using System.Security.Principal;
-
using System.IO;
-
-
namespace RemoteCredentials
-
{
-
class MainClass
-
{
-
[DllImport( "advapi32.dll", SetLastError = true )]
-
private static extern bool LogonUser( string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken );
-
-
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
-
private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr lpSource, int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr *arguments);
-
-
[DllImport( "kernel32.dll", CharSet = CharSet.Auto, SetLastError = true )]
-
private static extern bool CloseHandle( IntPtr handle);
-
-
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
-
public extern static bool DuplicateToken( IntPtr existingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr duplicateTokenHandle );
-
-
-
// logon types
-
const int LOGON32_LOGON_INTERACTIVE = 2;
-
const int LOGON32_LOGON_NETWORK = 3;
-
const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
-
-
// logon providers
-
const int LOGON32_PROVIDER_DEFAULT = 0;
-
const int LOGON32_PROVIDER_WINNT50 = 3;
-
const int LOGON32_PROVIDER_WINNT40 = 2;
-
const int LOGON32_PROVIDER_WINNT35 = 1;
-
-
/// <summary>
-
/// The main entry point for the application.
-
/// </summary>
-
[STAThread]
-
static void Main(string[] args)
-
{
-
IntPtr token = IntPtr.Zero;
-
IntPtr dupToken = IntPtr.Zero;
-
-
bool isSuccess = LogonUser("userID", @"\\100.100.100.100\d$", "PassWd", LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token);
-
if (!isSuccess)
-
{
-
RaiseLastError();
-
}
-
-
isSuccess = DuplicateToken( token, 2, ref dupToken );
-
if( !isSuccess )
-
{
-
RaiseLastError();
-
}
-
-
WindowsIdentity newIdentity;
-
WindowsImpersonationContext impersonatedUser = null;
-
DirectoryInfo dirInfo;
-
FileInfo[] files;
-
-
try
-
{
-
-
newIdentity = new WindowsIdentity(dupToken);
-
impersonatedUser = newIdentity.Impersonate();
-
-
dirInfo = new DirectoryInfo(@"\\100.100.100.100\d$\Test folder");
-
files = dirInfo.GetFiles();
-
}
-
finally
-
{
-
impersonatedUser.Undo();
-
}
-
-
foreach( FileInfo file in files )
-
{
-
Console.WriteLine( file.FullName );
-
}
-
-
isSuccess = CloseHandle( token );
-
if( !isSuccess )
-
{
-
RaiseLastError();
-
}
-
Console.ReadLine();
-
}
-
-
// GetErrorMessage formats and returns an error message
-
// corresponding to the input errorCode.
-
public unsafe static string GetErrorMessage( int errorCode )
-
{
-
int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
-
int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
-
int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
-
-
int messageSize = 255;
-
string lpMsgBuf = "";
-
int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
-
-
IntPtr ptrlpSource = IntPtr.Zero;
-
IntPtr ptrArguments = IntPtr.Zero;
-
-
int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref lpMsgBuf, messageSize, &ptrArguments);
-
if( retVal == 0 )
-
{
-
// throw new ApplicationException(string.Format( "Failed to format message for error code '{0}'.", errorCode ) );
-
Console.WriteLine(string.Format("Failed to format message for error code '{0}'.", errorCode));
-
}
-
-
return lpMsgBuf;
-
}
-
-
private static void RaiseLastError()
-
{
-
int errorCode = Marshal.GetLastWin32Error();
-
string errorMessage = GetErrorMessage(errorCode);
-
-
//throw new ApplicationException( errorMessage);
-
Console.WriteLine(string.Format("Error: {0}", errorMessage));
-
}
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Bill Young via .NET 247 |
last post by:
I'm having all sorts of problems trying to move a file across the network using the File.Move command with a computer on the network. I have tried doing it with \\<server>\<share> and I tried mapping...
|
by: Eje |
last post by:
I have a webapplication on a server. One function is to
build txt-files. These files should then be moved to a
fixed directory on another computer in the company
network.
I have tried to use...
|
by: Matthew Eno |
last post by:
I have a problem where I'm developing some code that moves files from
one place to another (on the same drive) with the destination file
having a new name.
What's happening is that the line of...
|
by: dermot |
last post by:
I have wrriten a small windows service application in visual studio
..net 2003 which listens for incoming FTP files.
These files would overwrite over time due to duplicate file names.
However any...
|
by: Andy Klare |
last post by:
I am looking for a way to copy a file from a local drive on the server to a
network share but I need to authenticate as a user that has access to the
network share. How can I tie a different set...
|
by: =?Utf-8?B?VG9t?= |
last post by:
Hi,
I need to copy and/or move files across servers to the other side of my
firewall. I was wondering if anyone can tell me what port(s) I will need to
open to run these methods in my C#...
|
by: coffeebasket |
last post by:
I have created an Windows Service application, and it is suppose to
move downloaded files from localdir to a network dir but it fails. The
same code in a consol application works just fine. I have...
|
by: jaeden99 |
last post by:
I was wandering if nyone has a script to move files older than x days old? i've seen several to delete, but I don't want to delete. I would like to create a backup of the files first verify with...
|
by: =?Utf-8?B?cm9nZXJfMjc=?= |
last post by:
hey, I've seen this problem many times over the net, and now I'm one of them.
simple code really.
File.Move(RemoteFolder, MoveImportedFilesTo);
where
RemoteFolder =...
|
by: Gina_Marano |
last post by:
Hey all,
It appears that if I File.Move a large file 1GB from one network
location to another location it is really slow.
example.
from: \\myserver\myfolder\a\largefile.txt
to:...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |