我可以访问网络路径上的文件,但我无法保存到相同的路径(C#)(asp.net)

时间:2015-07-30 14:31:06

标签: c# asp.net comexception

此声明在访问网络中的文件时没有问题。

  MyApp = new Excel.Application();
  MyApp.Visible = false;
  MyBook = MyApp.Workbooks.Open("//NetworkFolderPath/File.xlsx"); //This line
  MySheet = (Excel.Worksheet)MyBook.Sheets[1]; 

当我完成编辑文件并尝试使用“SaveCopyAs()”方法保存文件时出现问题。

  MyBook.SaveCopyAs("//NetworkFolderPath/File2.xlsx");
  MyBook.Close(0);

我得到的例外是

An exception of type 'System.Runtime.InteropServices.COMException' occurred in Application.dll but was not handled in user code

Additional information: Microsoft Excel cannot access the file    '//NetworkFolderPath/File2.xlsx'. There are several possible reasons:

1. The file name or path does not exist.

2. The file is being used by another program.

3. The workbook you are trying to save has the same name as a currently open  workbook.

对于数字1:该文件确实存在于文件夹中,我通过路径访问该文件,因此我排除了数字1。

对于2号:我不确定是否可能是这个原因,一些解释会很好

对于3号:我给了工作簿一个不同的名字,但我不确定这是否能解决问题。

我真的不确定从哪里开始。感谢所有帮助。

2 个答案:

答案 0 :(得分:0)

当启用“脱机文件”并且缓存中存在损坏时,我发现此path does not exist错误。尝试使用the instructions重置缓存。

答案 1 :(得分:0)

尝试一下,它节省了我2周的时间,应用程序将pdf保存在sahredpath中,具有安全ID(凭据) 如果您有任何疑问,请在下面评论!很高兴为您提供帮助

============================ 公共课模拟     {         [DllImport(“ advapi32.dll”,SetLastError = true)]         私有静态外部int LogonUser(字符串lpszUsername,字符串lpszDomain,字符串lpszPassword,                                             int dwLogonType,int dwLogonProvider,out int phToken);

    [DllImport("kernel32.dll")]
    private static extern int FormatMessage(int dwFlags, string lpSource, int dwMessageId, int dwLanguageId,
                                            StringBuilder lpBuffer, int nSize, string[] Arguments);


    private const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;
    private const int LOGON32_PROVIDER_DEFAULT = 0;
    private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;

    private static WindowsImpersonationContext winImpersonationContext = null;

    public static void ImpersonateUser(string domain, string userName, string password)
    {

        //Benutzer einloggen
        int userToken = 0;

        bool loggedOn = (LogonUser(userName, domain, password, LOGON32_LOGON_NETWORK_CLEARTEXT,
                                    LOGON32_PROVIDER_DEFAULT, out userToken) != 0);

        if (loggedOn == false)
        {
            int apiError = Marshal.GetLastWin32Error();
            StringBuilder errorMessage = new StringBuilder(1024);
            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, null, apiError, 0, errorMessage, 1024, null);
            throw new Exception(errorMessage.ToString());
        }

        WindowsIdentity identity = new WindowsIdentity((IntPtr)userToken);
        winImpersonationContext = identity.Impersonate();

    }

    public static void UndoImpersonation()
    {
        if (winImpersonationContext != null)
        {
            winImpersonationContext.Undo();
        }
    }
}

2。致电模拟

        Impersonate.ImpersonateUser("domain", "user name", "password");
        //Your Code as the new User
        DirectoryInfo _dirInfo = new DirectoryInfo(@"file path");
        FileInfo[] _files = FileExtension.GetFilesByExtensions(_dirInfo, ".xls", ".xlsx").ToArray();
        Impersonate.UndoImpersonation();     

=============================

参考:-https://forums.asp.net/t/2126720.aspx?Access+Denied+while+reading+writing+directory+in+Network+File+Share+through+Service+Account