File copy from Remote machine probelm

Discussion in 'ASP .Net' started by Bhavesh, Jan 5, 2008.

  1. Bhavesh

    Bhavesh Guest

    Hi everybody,

    I have problem in copying file from DatabaseServer to WebServer.Both
    machines are on LAN.

    Let me explain in details what i am doing & wht i want to achieve...

    I am using sqlserver 2000 & asp.net 2005 for my project..

    I have written one stored procedure which creates one Text file on
    database server.. & i want to copy that file in virtual path of my
    project on webserver..

    I am using following statement to copy a file..

    strTextFileName = "E:\DestinationReportText\"


    File.Copy(ConfigurationManager.AppSettings("ExportToTextFolderForCopy")
    & ViewState("ReportName") & ".txt", strTextFileName)

    and in web.config following is the value
    <appSettings>
    <add key="ExportToTextFolderForCopy" value="\\DBServerCompName
    \Report_Text\" />
    </appSettings>

    When i debug code on webserver then everything works well, but when i
    run this code from IIS on same machine then it gives following
    Error ..
    /*************************************************************
    Server Error in '/www' Application.
    --------------------------------------------------------------------------------

    Logon failure: unknown user name or bad password.

    Description: An unhandled exception occurred during the execution of
    the current web request. Please review the stack trace for more
    information about the error and where it originated in the code.

    Exception Details: System.IO.IOException: Logon failure: unknown user
    name or bad password.
    ***********************************************************/

    and to solve this I have given Full permission for "Report_Text"
    folder to admin user, to anonymous user, & tried other lots of
    stuffs,, but cound not make it to work...

    is any one have idea on this ?
     
    Bhavesh, Jan 5, 2008
    #1
    1. Advertisements

  2. Bhavesh

    Toze Guest

    you must give permission on the remote machine... to the user that the iis
    is running (IUSR)

    Exemple:
    IIS HOST - MACHINE NAME - IISHOST
    FILE HOST - MACHINE NAME - FILEHOST
    You must giver permission in the folder on the FILEHOST to
    IISHOST\IUSR_IISHOST
     
    Toze, Jan 6, 2008
    #2
    1. Advertisements

  3. Here is the code you can use to access shared file with given
    username/password


    //used in calling WNetAddConnection2
    [StructLayout(LayoutKind.Sequential)]
    public struct NETRESOURCE
    {
    public int dwScope;
    public int dwType;
    public int dwDisplayType;
    public int dwUsage;
    [MarshalAs(UnmanagedType.LPStr)]
    public string lpLocalName;
    [MarshalAs(UnmanagedType.LPStr)]
    public string lpRemoteName;
    [MarshalAs(UnmanagedType.LPStr)]
    public string lpComment;
    [MarshalAs(UnmanagedType.LPStr)]
    public string lpProvider;
    }
    //WIN32API - WNetAddConnection2
    [DllImport("mpr.dll",
    CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern int WNetAddConnection2A(
    [MarshalAs(UnmanagedType.LPArray)] NETRESOURCE[] lpNetResource,
    [MarshalAs(UnmanagedType.LPStr)] string lpPassword,
    [MarshalAs(UnmanagedType.LPStr)] string lpUserName,
    int dwFlags);
    [DllImport("mpr.dll",
    CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern int WNetCancelConnection2A(
    [MarshalAs(UnmanagedType.LPStr)] string lpName,
    int dwFlags, int fForce);

    private byte[] GetFSMSFile(string sFile)
    {
    NETRESOURCE[] nr = new NETRESOURCE[1];
    nr[0].lpRemoteName = "\\SHAREDSERVER\FOLDER";
    nr[0].lpLocalName = ""; //mLocalName;
    nr[0].dwType = 1; //disk
    nr[0].dwDisplayType = 0;
    nr[0].dwScope = 0;
    nr[0].dwUsage = 0;
    nr[0].lpComment = "";
    nr[0].lpProvider = "";
    int iErr = WNetAddConnection2A(nr,UserPassword, ShareUser, 0);
    if (iErr > 0)
    throw new Exception("Can not connect to shared folder");
    FileStream st = null;
    try
    {
    st = new FileStream("\\SHAREDSERVER\FOLDER\myfile.txt",
    FileMode.Open);
    int iLen = (int)st.Length;
    byte []b = new byte[iLen];
    st.Read(b, 0, iLen);
    return b;
    }
    finally
    {
    if( st != null )
    st.Close();
    WNetCancelConnection2A(_sFSMSShare, 0, -1);
    }
    }
     
    George Ter-Saakov, Jan 8, 2008
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.