Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created a window service that moves all file from one folder to other.
my problem is that

i want to move file within LAN, i.e., from one system to other system share folder. i am unable to do it. how can i do this.
how to give credentials so that i can move

below is my code

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
using System.Security.Principal;


namespace filemovet
{
    public partial class Service1 : ServiceBase
    {
        public Timer t = null;
        public Service1()
        {
            InitializeComponent();

        }

        protected override void OnStart(string[] args)
        {
            t = new Timer();
            this.t.Enabled = true;
            this.t.Elapsed += new ElapsedEventHandler(t_tick);
            this.t.Interval = 10000;
        }

        protected override void OnStop()
        {
            this.t.Enabled = false;
        }
        public void t_tick(object sender, EventArgs e)
        {
            try
            {
               

               
                String dest_path = "D:\\output1";
                DirectoryInfo dirInfo = new DirectoryInfo(dest_path);
                if (dirInfo.Exists == false)
                    Directory.CreateDirectory(dest_path);
                List<string> allFiles = Directory.GetFiles("C:\\input\\", "*.*", SearchOption.AllDirectories).ToList();
                foreach (string file in allFiles)
                {
                    FileInfo mFile = new FileInfo(file);
                    if (File.Exists(dirInfo + "\\" + mFile.Name))
                    {

                        File.Delete(dirInfo + "\\" + mFile.Name);
                    }
                    // mFile.CopyTo(dirInfo + "\\" + mFile.Name);
                    mFile.MoveTo(dirInfo + "\\" + mFile.Name);
                }


            }
            catch (Exception ex)
            {

            }
        }
    }
}
Posted
Updated 4-Apr-13 2:26am
v2
Comments
[no name] 4-Apr-13 8:27am    
"i am unable to do it", why do you say that you are unable to do this?
nishant ranjan13 4-Apr-13 8:35am    
means i am not getting the way how to do this as m new to window form
[no name] 4-Apr-13 8:55am    
means what exactly. What exactly is wrong with the code that you already have? means you have to explain what the problem actually is.
nishant ranjan13 4-Apr-13 11:39am    
the problem is i am able to move within my system but not within the LAN i.e., if the folder is in my system then it moves but what i want to do is to move file to other system folder within my LAN i.e the other system is in LAN and it has the folder
[no name] 4-Apr-13 12:51pm    
Okay and? That in no way explains what your problem actually is. What errors are you getting? What exceptions are you getting? Does anything show up in your event logs? Are you running your service under the Network account or Local system? What do you see when you run your code?

C#
if (File.Exists(dirInfo + "\\" + mFile.Name))
{

    File.Delete(dirInfo + "\\" + mFile.Name);
}


Doesn't this mean that you delete the file?
And then you try to move it....

Or am I wrong?
 
Share this answer
 
v2
Comments
[no name] 4-Apr-13 8:59am    
Good catch
Pheonyx 4-Apr-13 9:04am    
I read that as it removes a file from the destination directory if it already exists .
nishant ranjan13 4-Apr-13 11:33am    
yes you are right. as i am moving file so if the file already exists i am deleting it at the destination folder before moving
You have to use Impersonate while accessing over networks.
A basic usage will be
C#
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity identity = new WindowsIdentity(username, password);
WindowsImpersonationContext context = identity.Impersonate();
try
{
    File.Copy(FilePathToBeCopiedFrom, FilePathToBeCopiedAt, true);
}
catch
{
    context.Undo();
}

But i will suggest you have a look at this link and use it in your service;
C# accessing or copying files over a network that requires authentication[^]

Good luck,
OI
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900