Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Asp.net + c# problem

Options
  • 14-05-2007 10:06am
    #1
    Registered Users Posts: 357 ✭✭


    Basically i want to copy a folder from one server to another

    This code works when the folders are on the local hard drive but not across servers. Can anyone help me to get it to work across servers
    <%@ Page Language = "c#" %>
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.IO" %>



    <script runat="server" language="C#" >


    public static int CopyAll(DirectoryInfo sourceDir, DirectoryInfo targetDir)
    {
    DirectoryInfo source = sourceDir;
    DirectoryInfo target = targetDir;

    if (Directory.Exists(target.FullName) == false)
    {
    Directory.CreateDirectory(target.FullName);
    }
    foreach (FileInfo fi in source.GetFiles())
    {
    Console.WriteLine(@Copying {0}\{1}, target.FullName, fi.Name);
    fi.CopyTo(Path.Combine(target.ToString(), fi.Name),true);


    }
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
    {
    DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
    CopyAll(diSourceSubDir,nextTargetSubDir);

    }

    return 1;
    }

    </script>



    <html>

    <head>
    </head>
    <body>

    <br>
    <font size = 20>Copy Folder</font>
    <br>

    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.IO" %>
    <%


    string sourceDir = Request.QueryString.Get("source");
    string targetDir = Request.QueryString.Get("destination");
    DirectoryInfo a = new DirectoryInfo(sourceDir);
    DirectoryInfo b = new DirectoryInfo(targetDir);
    int x = CopyAll(a,b);
    Response.Write("It Works Sweet Jesus It Works");
    %>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <input type=button value="Back" align="left" onClick="window.location='./directoryc.aspx'">


    </body>

    </html>

    This is the error i get

    System.IO.IOException: The process cannot access the file '\\********\c$\clntprd30\TEST\test2.txt' because it is being used by another process. (I took out the name of the replica)


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Sounds like FileInfo.CopyTo isn't releasing the file. Can you give us the full error trace?


  • Registered Users Posts: 357 ✭✭apoch632


    [IOException: The process cannot access the file '\\irsreplica03\c$\clntprd30\TEST\test2.txt' because it is being used by another process.]
    System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +2015133
    System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite) +488
    System.IO.FileInfo.CopyTo(String destFileName, Boolean overwrite) +21
    ASP.replicaping_copyfolder_aspx.CopyAll(DirectoryInfo sourceDir, DirectoryInfo targetDir) in c:\Inetpub\wwwroot\ReplicaPing\copyfolder.aspx:22
    ASP.replicaping_copyfolder_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Inetpub\wwwroot\ReplicaPing\copyfolder.aspx:59
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +2068219
    System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
    System.Web.UI.Page.Render(HtmlTextWriter writer) +26
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1896


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    I'm not sure as I don't have time to replicate this but you could try something like the following:
    foreach (FileInfo fi in source.GetFiles())
    {
        Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
        [b]using (FileStream fs = fi.Create()) {}[/b]
        fi.CopyTo(Path.Combine(target.ToString(), fi.Name),true);
    }
    

    Using a FileStream should open and close a filestream for the file which will, in theory (:p), release the file from the process. So while this code mightn't work off the bat its an avenue worth exploring. You could also try
    foreach (FileInfo fi in source.GetFiles())
    {
        Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
        [b]using (FileStream fs = fi.Create()) 
        {[/b]
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name),true);
        [b]}[/b]
    }
    


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Evil Phil wrote:
    foreach (FileInfo fi in source.GetFiles())
    {
        Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
        [b]using (FileStream fs = fi.Create()) 
        {[/b]
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name),true);
        [b]}[/b]
    }
    
    That shouldn't have any effect other than making it impossible to copy the file ;) Also, wouldn't calling fi.Create() overwrite the existing file? That wouldn't be fun.

    Are you accessing test2.txt at any stage in your program? Are you creating it by hand, or via code? Does any application have it open?


  • Registered Users Posts: 357 ✭✭apoch632


    I created test2.txt by hand and placed it in the folder

    Nothing else has it open.

    Nothing tries to access it until it reaches this piece of code.

    I get the same error for any subfolder(Whatever file(s) it contains appear as the error instead of test2.txt) I try to copy


  • Advertisement
  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Do you have read and write permissions in both the destination and original folders?

    You can test this by trying to create a file and delete the same file in each folder through code.


  • Registered Users Posts: 357 ✭✭apoch632


    Yeah i have read/write permissions.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Try using the File.Move method.

    Also, print out both the full source path and directory path to make sure their valid.

    How are you creating the source/directory paths? It's quite possible your webapp doesn't have permission to access a UNC style path.


Advertisement