我正在尝试将我的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,false,Excel.XlSaveAsAccessMode.xlNoChange,null);
此代码完美地工作8/10次,并抛出网络错误(未找到网络路径)2次.当远程路径抛出此错误时,我在运行此代码的计算机上使用run命令打开上面的远程路径,我可以正常访问它.关闭手动打开的文件夹并重新运行代码正在解决问题.可能是什么问题?我在这里做错了吗?
解决方法
检查以下事项
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