Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<add key="ProofFileSavePath" value="D:\\Websites\xyz\ABC\Files\" />



C#
private async Task DownloadPdf(string proofFileUrl, long batchFileDetailId, string renderedRawId, bool isNewFileForRenderedRawId, string masterProofFileDownloadDate)
{
    var methodName = "DownloadPdf";
    if (!string.IsNullOrEmpty(proofFileUrl))
    {
        if (isNewFileForRenderedRawId)
        {
            using (var client = new WebClient())
            {
                var uri = new Uri(proofFileUrl);
                client.DownloadFileCompleted += DownloadFileCallback;
                var fileInfo = new FileInfo(uri.AbsolutePath);
                if (!string.IsNullOrEmpty(fileInfo.Extension))
                {
                    var proofFileName = Path.GetFileName(uri.LocalPath);
                    client.QueryString.Add(Constants.BatchFileDetailId, batchFileDetailId.ToString());
                    client.QueryString.Add(Constants.ProofFileName, proofFileName);
                    client.QueryString.Add(Constants.RenderedRawId, renderedRawId);
                    var proofFileSaveFullPath = Path.Combine(CommonUtil.ProofFileSavePath, proofFileName);
                    await client.DownloadFileTaskAsync(uri, proofFileSaveFullPath);
                }
            }
        }
        else
        {
            UpdateIsProofReadyFlag(batchFileDetailId, proofFileUrl, renderedRawId, masterProofFileDownloadDate, false);
        }
    }
    else
    {
        _loggingHelper.Log(LoggingLevels.Error,
            "Class: " + _className + " :: " + methodName + " :: " +
            "Proof File Url not found");
    }
}


What I have tried:

XML
<add key="ProofFileSaveNetworkPath" value="\\ServerName\ShareName\" />
		<add key="ProofFileSaveNetworkUid" value="yourUsername" />
		<add key="ProofFileSaveNetworkpwd" value="yourPassword" /> 


C#
private async Task DownloadPdf(string proofFileUrl, long batchFileDetailId, string renderedRawId, bool isNewFileForRenderedRawId, string masterProofFileDownloadDate)
        {
            var credentials = new NetworkCredential(CommonUtil.ProofFileSaveNetworkUid, CommonUtil.ProofFileSaveNetworkpwd);
            CredentialManager.SaveCredentials("NetworkCredentials", credentials);

            var methodName = "DownloadPdf";
            if (!string.IsNullOrEmpty(proofFileUrl))
            {
                if (isNewFileForRenderedRawId)
                {
                    var networkFolder = CommonUtil.ProofFileSaveNetworkPath;
                    
                    using (var client = new WebClient())
                    {
                        var uri = new Uri(proofFileUrl);
                        client.DownloadFileCompleted += DownloadFileCallback;
                        var fileInfo = new FileInfo(uri.AbsolutePath);
                        if (!string.IsNullOrEmpty(fileInfo.Extension))
                        {
                            var proofFileName = Path.GetFileName(uri.LocalPath);
                            client.QueryString.Add(Constants.BatchFileDetailId, batchFileDetailId.ToString());
                            client.QueryString.Add(Constants.ProofFileName, proofFileName);
                            client.QueryString.Add(Constants.RenderedRawId, renderedRawId);
                            var proofFileSaveFullPath = Path.Combine(networkFolder, proofFileName);

                            if (credentials != null)
                            {
                                client.Credentials = new NetworkCredential(credentials.UserName, credentials.Password);
                            }

                            await client.DownloadFileTaskAsync(uri, proofFileSaveFullPath);
                        }
                    }
                }
                else
                {
                    UpdateIsProofReadyFlag(batchFileDetailId, proofFileUrl, renderedRawId, masterProofFileDownloadDate, false);
                }
            }
            else
            {
                _loggingHelper.Log(LoggingLevels.Error,
                    "Class: " + _className + " :: " + methodName + " :: " +
                    "Proof File Url not found");
            }
        }
Posted
Updated 12-Aug-23 17:30pm
v3
Comments
Andre Oosthuizen 11-Aug-23 13:46pm    
Ok, great!. Did you have a question for us regarding this?
mahesh4646 11-Aug-23 14:48pm    
I want to use Network folder to store the dowanload file form this methord, but now i'm using a local file path like this <add key="ProofFileSavePath" value="D:\\Websites\xyz\ABC\Files\">. i need to replace this path with a network folder which has userid and password.
OriginalGriff 11-Aug-23 15:33pm    
And?
What have your tried?
Where are you stuck?
What help do you need?

Just repeating "I want ..." doesn't add any information to your original query.
[no name] 12-Aug-23 11:01am    
Tell the "web site" to FTP the file to itself; probably a security violation otherwise.
PIEBALDconsult 12-Aug-23 12:05pm    
Save it locally and then move it to where you want it if it succeeds.

1 solution

I did a Google Search: c# write file to network path[^]

And this first result looks like what you need: How to save data to network path on .NET?[^].

Here is another from CodeProject: Create text file on a network shared location[^]

If not, there are other solutions.
 
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