简体   繁体   English

当我尝试在特定文件夹vb.net中复制Excel时,拒绝访问路径”

[英]Access to the path '' is denied when i try to copy a Excel in specific folder vb.net

i try to copy a file from the server on the specific folder when press a button, my code for copy the file are that: 我尝试在按下按钮时从服务器上特定文件夹中复制文件,我用于复制文件的代码是:

If System.IO.File.Exists(Server.MapPath("../../TempDownloads/" & SampleOrder & ".xls")) = True Then
            System.IO.File.Copy(Server.MapPath("../../TempDownloads/" & SampleOrder & ".xls"), "\\art-fs02\07_ART_ECO\12_Samples\03_Samples_order\999_MoraruVladutBOM_Test\" & SampleOrder & ".xls", True)
        Else
            LBL_Error.Text = "There is no file to be copied !"
            GC.Collect()
            Exit Sub
        End If

Ok, all good, i have two application with different referinces and differents imports, but at first application work to copy my file in specific folder ("\\art-fs02\\07_ART_ECO\\12_Samples\\03_Samples_order\\999_MoraruVladutBOM_Test\\") but when i try to another applicatiob files doesn't work. 好的,一切都很好,我有两个具有不同引用和不同导入的应用程序,但是首先应用程序将我的文件复制到特定的文件夹中(“ \\ art-fs02 \\ 07_ART_ECO \\ 12_Samples \\ 03_Samples_order \\ 999_MoraruVladutBOM_Test \\”),但是当我尝试另一个应用文件不起作用。 All two application are on server. 所有两个应用程序都在服务器上。

When i put the same code in page get me error like that: 当我在页面中输入相同的代码时,出现如下错误:

Access to the path '\\art-fs02\\07_ART_ECO\\12_Samples\\03_Samples_order\\999_MoraruVladutBOM_Test\\Test.xls' is denied. 拒绝访问路径'\\ art-fs02 \\ 07_ART_ECO \\ 12_Samples \\ 03_Samples_order \\ 999_MoraruVladutBOM_Test \\ Test.xls'。

I put a image with References for first application where my code work and references for second application and where doesn't work. 我在第一个应用程序的引用中放置了一个图像,其中我的代码有效,第二个应用程序的引用却不起作用。

在此处输入图片说明

I don't know why on first application my code work, and copy the file, and the second application doesn't work. 我不知道为什么在第一个应用程序上我的代码可以正常工作,然后复制文件,而第二个应用程序无法正常工作。

I put all my code for page above: 我把我所有的代码放在上面的页面:

Imports System.Data
Imports System.IO
Imports OfficeOpenXml
Imports System.Globalization
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Configuration

Partial Class TCO_Orders_TestCopy
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SampleOrder As String = "SO-Test"
        Dim Qty As String = "55"

        SampleOrder = SampleOrder.Remove(0, 3)

        Dim cn As New OleDbConnection
        Dim cm As New OleDbCommand
        cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server.MapPath("../../FolderBOM/BOM2.xls") & ";Extended Properties=""Excel 12.0;HDR=Yes;""")
        cn.Open()
        With cm
            .Connection = cn
            .CommandText = "UPDATE [ARO list$] set info = '" & SampleOrder & "' where [SO-info] ='DVM SO no.'"
            cm = New OleDbCommand(.CommandText, cn)
            cm.ExecuteNonQuery()
            cn.Close()

            cn.Open()
            .CommandText = "UPDATE [ARO list$] set info = '" & SampleOrder & "' where [SO-info] ='Local SO no.'"
            cm = New OleDbCommand(.CommandText, cn)
            cm.ExecuteNonQuery()
            cn.Close()

            cn.Open()
            .CommandText = "UPDATE [ARO list$] set info = " & Qty & " where [SO-info] ='qty'"
            cm = New OleDbCommand(.CommandText, cn)
            cm.ExecuteNonQuery()
            cn.Close()
        End With

        Try
            My.Computer.FileSystem.CopyFile(
        Server.MapPath("../../FolderBOM/BOM2.xls"),
        Server.MapPath("../../TempDownloads/" & SampleOrder & ".xls"), Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
        Catch ex As Exception
            LBL_Error.ForeColor = System.Drawing.Color.Red
            LBL_Error.Text = "This SO has BOM created !"
            Exit Sub
        End Try

        If System.IO.File.Exists(Server.MapPath("../../TempDownloads/" & SampleOrder & ".xls")) = True Then
            System.IO.File.Copy(Server.MapPath("../../TempDownloads/" & SampleOrder & ".xls"), "\\art-fs02\07_ART_ECO\12_Samples\03_Samples_order\999_MoraruVladutBOM_Test\" & SampleOrder & ".xls", True)
        Else
            LBL_Error.Text = "There is no file to be copied !"
            GC.Collect()
            Exit Sub
        End If
    End Sub
End Class

我本打算对此发表评论,但是我还没有代表发表评论,所以我被迫改成回答,但是很多时候您遇到路径访问冲突或文件访问冲突的情况很多有时,它可能是无效高程的原因,并且不会让您在指定路径内执行任何操作,如果尝试在编辑器中运行它,则始终可以尝试在Admin Elevation中运行该编辑器,这样它将提升任何已完成的流程通过它以及您尝试测试的程序,或者您可以直接以Admin身份运行它,然后查看它是否正常运行,在此之前我已经遇到过几次此问题,并且大多数时候都是通过此问题解决的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM