C# Winforms Debug build works, Release fails.

Bryan Valencia 21 Reputation points
2021-02-02T20:31:22.963+00:00

Using SpreadsheetLight, but the failure is just creating a file. Here is the error from ProcessMonitor.

RELEASE MODE

ACCESS DENIED

> Desired Access: Read Attributes, Delete, Disposition: Open, Options: Non-Directory File, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a

When run in debug mode, I get:

DEBUG MODE

SUCCESS

> Desired Access: Read Attributes, Delete, Disposition: Open, Options: Non-Directory File, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened

It's VS 2019 .NET Framework 4.8 Built for Any CPU.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,298 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,227 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,196 Reputation points
    2021-02-03T11:42:11.547+00:00

    Hello,

    Couple of things

    (I did watch the video ,the longer one)

    And the short answer is this is a Windows or anti-virus permission issue which I talk about below

    • When creating a project with Visual Studio I suggest (doesn't have to be exactly this) using C:\DotNet following creating a VS solution under this path e.g. C:\DotNet\LowsesImporter. When creating C:\DotNet via Windows Explorer you should not need admin rights and if that is the case this is the first flag, insufficient permissions and if no admin rights great.
    • From the video, I noticed in release mode from Windows Explorer after double clicking the app didn't start which leads me to believe this is a Windows permission issue (and yeah it should do the same in debug configuration).

    What I did (as an admin on my box) in release mode
    Copied C:\OED\Dotnetland\VS2019\QuestionsSolution\SpreadSheetLight1\bin\Release*.* to C:\Users\paynek\SpreadSheetLight_KP, ran, success.

    Can you try this using a similar folder under C:\Users... (and when creating the folder did you need admin rights?), run by double clicking, if it doesn't run try again via right click on the .exe, select run as admin, what happens? If this also failed look at either Windows Defender or any other anti-virus software installed, in either case look at creating an exclusion rule to indicate your .exe is safe to run.

    Try an app manifest

    Add an app manifest, replace contents with the following, build run, answer Yes, what happens?

    <?xml version="1.0" encoding="utf-8"?>
    <assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
      <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
          <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
          </requestedPrivileges>
        </security>
      </trustInfo>
    
      <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
    
        </application>
      </compatibility>
    
    
    </assembly>
    

    Bottom line

    To be honest I never use release mode as all my applications are setup to write debug information (similar to this class project) on runtime exception where with release mode some information is not available. I have seen this before where there were about 30 users and a handful had the same error as you even in debug configuration were our engineers had to adjust active directory rules.

    My conclusion is

    Windows Active Directory is denying access (yeah it should also happen in debug mode too) or ant-virus app denying access, nothing to do with Visual Studio.

    Revised source

    Main difference is console vs windows form app and used Path.Combine to create the path/file name to create the excel file.

    using System;
    using System.IO;
    using SpreadsheetLight;
    
    namespace SpreadSheetLight1
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    using (var doc = new SLDocument())
                    {
                        Console.WriteLine($"Create {ExcelFileName}");
                        doc.SaveAs(ExcelFileName);
                        Console.WriteLine("Excel file created");
    
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    Console.WriteLine($"Access denied to: {ExcelFileName}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
    
                Console.ReadLine();
            }
    
            static string ExcelFileName => 
                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), 
                    "Excel1.xlsx");
        }
    }
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,196 Reputation points
    2021-02-02T21:20:23.63+00:00

    Hello,

    Using the following in VS2019, release mode an Excel file in the executable folder was generated as expected. So I could not replicate your issue.

    using SpreadsheetLight;
    
    namespace SpreadSheetLight1
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var doc = new SLDocument())
                {
                    doc.SaveAs("Excel1.xlsx");
                }
            }
        }
    }
    

  2. Bryan Valencia 21 Reputation points
    2021-02-03T00:26:25.707+00:00

    It's the error message I have already reported.

    I added a call to this procedure, built and ran it using debug and release.

    Debug: this code runs without error.
    Release: I get an ACCESS DENIED error.

    0 comments No comments