Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy file to remote location throws network path not found intermittently

I am trying to copy a file generated (Excel file) by my c# code into a remote network path to which I have access as below:

string folder = "\\\\testing-path\\Audit\\Reports";
if (!(Directory.Exists(folder + "\\" + DateTime.Now.ToString("MM-dd-yyyy") + "\\" + "Audit")))
{
    Directory.CreateDirectory(folder + "\\" + DateTime.Now.ToString("MM-dd-yyyy") + "\\" + "Audit");
}
folder = folder + "\\" + DateTime.Now.ToString("MM-dd-yyyy") + "\\" + "Audit";

if (File.Exists(folder + "\\Audit- " + fname + ".xlsx"))
{
    File.Delete(folder + "\\Audit- " + fname + ".xlsx");
}
string fileName = folder + "\\Audit- " + fname + ".xlsx";
wb.SaveAs(fileName,
    Excel.XlFileFormat.xlWorkbookDefault, null, null,
    false, false, Excel.XlSaveAsAccessMode.xlNoChange,
    null, null, null, null, null);

This code works perfectly fine 8/10 times and throws network error (Network path not found) other 2 times. When the remote path throws this error I open the above remote path using run command on the machine I run this code and I am able to access it as normal. Closing the folder opened manually and re-running the code is solving the issue. What could be the issue? Am I doing something wrong here?

PS: I also tried to save the excel file onto desktop and then use File.Copy and there is no change to this intermittent behaviour.

like image 861
TRR Avatar asked Nov 12 '22 20:11

TRR


1 Answers

Check the following things

1) First you have to check whether machine is on or off
2) Then check you have access rights to put the file in that particular folder and dont
   put it inside ' C ' Drive because C is not accessible for other users in network put it inside D or E drive.
3) If Folder is Missing You have to Create A Folder first

and what is ' WB '

 if(!Directory.Exists("\\\\testing-path\\Audit\\Reports"))
    Directory.Create("\\\\testing-path\\Audit\\Reports");

Use

                                                          FilePath     Byte 

 File.ReadAllBytes("FilePath/FileName.Extension",byte) //  D:\\Test.xls,12878

then in Remote Location FilePath Byte

 File.WriteAllBytes("FilePath/FileName.Extension",byte)//   D:\\Test123.xls,12878

Check this Link

Accessing paths in Remote Machine : http://www.codeproject.com/Questions/184633/Connect-to-a-shared-folder-using-ip-address-in-vb6

like image 163
SAR Avatar answered Nov 14 '22 22:11

SAR