将文件复制到远程位置会引发间歇性未找到的网络路径

时间:2013-10-08 06:27:06

标签: c# export-to-excel

我正在尝试将我的c#代码生成的文件(Excel文件)复制到我可以访问的远程网络路径中,如下所示:

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);

此代码完美地工作8/10次,并引发网络错误(未找到网络路径)2次。当远程路径抛出此错误时,我在运行此代码的计算机上使用run命令打开上面的远程路径,我可以正常访问它。关闭手动打开的文件夹并重新运行代码正在解决问题。可能是什么问题?我在这里做错了吗?

PS:我还试图将excel文件保存到桌面上,然后使用File.Copy,这种间歇性行为没有变化。

1 个答案:

答案 0 :(得分:1)

检查以下内容

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

什么是'WB'

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

使用

                                                          FilePath     Byte 

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

然后在远程位置                                                                FilePath字节

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

检查此链接

访问远程计算机中的路径:http://www.codeproject.com/Questions/184633/Connect-to-a-shared-folder-using-ip-address-in-vb6