none
Error in moving file from a folder to new folder in windows service using visual c++ RRS feed

  • Question

  • Hi all,

    I'm a student and currently learning visual c++. I've experience in c++ and java but never worked in visual c++ before. So my experties are not very good. Currently I'm writing a service for my project to copy files of one particular folder to new folder with a timer. But I'm unable to do so. My code to write file is:

    static
     void
     OnTimedEvent( Object^ /*source*/
    , ElapsedEventArgs^ /*e*/
     )
    {
    eventLogger->WriteEntry("FTPLoader Service Timer Expire" );
    String^ FromDirectory = configData->getLocalDir();
    String^ ToDirectory = configData->getBackupDir();
    if ( FromDirectory->Length !=0 && ToDirectory->Length !=0)
    {
    // Create a reference to the source directory.
    try
    {
    // Only get files that match the wildcard character pattern.
    array<System::String^ >^ dirs = Directory::GetFiles(FromDirectory);
    // Iterate through the file collection.
    Collections::IEnumerator^ myEnum = dirs->GetEnumerator();
    while (myEnum->MoveNext())
    {
    // Get the source file path and the destination file path.
    String^ SourceFilePath = static_cast <String ^>(myEnum->Current);
    String^ TargetFilePath = String::Concat(ToDirectory ,Path::GetFileName(SourceFilePath));
    FileIOPermission^ f3 = gcnew FileIOPermission( PermissionState::Unrestricted );
    f3->AllFiles = FileIOPermissionAccess::AllAccess;
    try
    {
    f3->Demand();
    }
    catch (Exception^ e)
    {
    eventLogger->WriteEntry(e->Message);
    }
    File::Move(SourceFilePath, TargetFilePath);

    }catch ( Exception ^ e)
    eventLogger->WriteEntry("Can't move file from: " + SourceFilePath + " to: " + TargetFilePath);
    eventLogger->WriteEntry(e->ToString());
    }
    }
    In the above code, eventLogger is object of my own logging class which appends a text file for logging.

    I've also tried this without FileIOPermission Object but same error occures which is:
    1/25/2010 12:13:14 PM => FTPLoader Service Timer Expire
    1/25/2010 12:13:14 PM => Can't move file from: C:\temp\ftploader\loader\untitled.PNG to: c:\temp\ftploader\backup\untitled.PNG
    1/25/2010 12:13:14 PM => 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.File.Move(String sourceFileName, String destFileName)
    at TimerDetail.Timer1.OnTimedEvent(Object , ElapsedEventArgs )
    Can anyone tell me where I'm making mistake and how to resolve it?? I'll be very thankful to you.
    Wednesday, January 27, 2010 10:56 AM

Answers

  • What account you are using to run this service? Loacl Service? System? Network Service? or your own?
    Check if this account actually have rights to these folders. I think it is the source of your problem.

    Wednesday, January 27, 2010 12:04 PM