Tag Archives: c#

C ා programming encountered an object reference is required for the non-static field, method, or property error

When you create static methods in your form code, the source code looks like this

public int imageNum=0;

。。。。。。

 public static int button1_clicknum()
        {
            return imageNum;
        }

This is followed by an error such as the following. The error is caused by the fact that the static method cannot directly access the non-static member, and the non-static field needs to be referenced, which can be a private member of an instance. To correct this, change imagenum to a static member.
Public static int imageNum = 0;

WCF error “an error occurred when verifying security for the message”… “

This is the problem encountered when deploying a project on the client machine in June. C and S can’t be connected to each other. If they are installed on one machine, it will be ok. Finally found that the two machine clock is out of sync, the difference of a few hours, after the correction of time is OK. The problem took a day to solve.

Reproduced in: https://www.cnblogs.com/Gildor/archive/2009/09/05/1560825.html

ISDEV : fatal error -6109: Internal build error

To use InstallShield2015 Premier and installshields to shields up to the last occurrence of “ISDEV: Fatal error-6109: Internal build error”.

But create a new project and drag a few files into the project without prompting
Step by step, the problem was found that all files in the project with error-1069 prompt were deleted, and the prompt disappeared
Later, I added DLLS that were not dependent on other libraries to the project first, and then added DLLS that were dependent on other libraries. After several rounds of adjustment, it was normal at last
InstallShield has all these years of products have this problem

supplement:
later I will Options -> .NET -> In default.net Scan At Build Component Setting, the option is changed to None, (as shown in the figure below)
delete all files in the project again, add them again, and Build again. is OK

Error cs1002:; expected error resolution

This error usually occurs because the C# code in the front page has fewer semicolons, or fewer parentheses, causing the compiler to fail. Double check that the C# code on the page is written correctly.
I made this error because the code at @{} in the foreground page is missing a parenthesis. Add parentheses to solve the problem.

Reproduced in: https://www.cnblogs.com/MrZheng/p/10186059.html

net core HTTP Error 502.5 – ANCM Out-Of-Process Startup Failure

Personal blog: Yedajiang44.com/blog
Vs was updated yesterday. When opening the project and running today, it directly reported the following error:

HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure
Common causes of this issue:
The application process failed to start
The application process started but then stopped
The application process started but failed to listen on the configured port
Troubleshooting steps:
Check the system event log for error messages
Enable logging the application process' stdout messages
Attach a debugger to the application process and inspect
For more information visit: https://go.microsoft.com/fwlink/?LinkID=808681

Solution: just reinstall the corresponding NET Core SDK of the project, and remember to restart!

How to launch powershell script in C#

If you want to start a powershell script in a CSharp application, you don’t have to construct a cmd command line to start the script.

You can use the following example to make your life easier:

The variable “script” is the full path to the powershell script.
The variable “parameters” is an instance of an IDictionary type, which contains a set of parameter keys/values.

            using (var powerShellInstance = PowerShell.Create())
            {
                //Prepare powershell execution
                powerShellInstance.AddCommand(script);
                powerShellInstance.AddParameters(parameters);

                //Execute powershell command and get the results
                var results = powerShellInstance.Invoke();

                var errors = powerShellInstance.Streams.Error;
                var sb = new StringBuilder();

                if (errors.Count > 0)
                {
                    foreach (var error in errors)
                    {
                        sb.Append(error);
                    }
                    errorResult = sb.ToString();
                }
                else
                {
                    foreach (var result in results)
                    {
                        sb.AppendLine(result.ToString());
                    }
                    executionResult = sb.ToString();
                }

                return errors.Count == 0;
            }

Update:2015-07-01
I’m having a problem executing a powershell script in the logon server.
Actually, the application uses a system account to execute the powershell script.
But the account does not have enough privileges to run the script.

The exception is:PSSecurityException
Here are the details of the error:

Message: AuthorizationManager check failed.
InnerException stack trace:    
at System.Management.Automation.AuthorizationManager.ShouldRunInternal(CommandInfo commandInfo, CommandOrigin origin, PSHost host)
InnerException: A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your computer. Do you want to run xxx.ps1?

I searched the internet for information. It is so important to repeat the same mistakes! First, I copied the service account into the integrated environment by removing it from the “Administrators” group.
You can go to the “Local Users and Groups”, then “Groups”, then “Administrators” group. Select the service account and delete it from the group. Well, I found that the problem is related to the enforcement policy on the server. I’ve tested it with the enforcement policy.
You can use open powershell.exe on the server.
Execute the command:

Get-ExecutionPolicy

You can even verify that a particular user is enforcing the policy.
You will need to open powershell.exe from your service account to run it.
Then execute the command:

Get-ExecutionPolicy -Scope:CurrentUser

 

In my server, the enforcement policy is set to unlimited in the LocalMachine range.

There are 7 enforcement policies in total.
Default:This is equal to Restricted
Restricted: Do not load configuration files or run scripts. This is the default.
AllSigned: Requires all scripts and configuration files to be signed by a trusted publisher, including scripts written on your local machine.
remotesizable: Requires all scripts and configuration files downloaded from the Internet to be signed by a trusted publisher.
unrestricted:Load all configuration files and run all scripts. If you run an unsigned script downloaded from the internet, you will be prompted for permission before running the script.
Bypass: No blocking, warnings or prompts.
Undefined:Deletes the currently assigned enforcement policy from the current scope. This parameter does not delete enforcement policies set within the Group Policy range.

0 has 5 ranges:
Process
CurrentUser
LocalMachine
UserPolicy
MachinePolicy

0 is actually the enforcement policy preventing the service account from running the script correctly. So I need to change the enforcement policy. In the end, the bypass method meets my needs. But I don’t apply this enforcement policy to all types of users within the local machine. So I only apply the bypass enforcement policy to the service account.

The commands used are:

Set-ExecutionPolicy -Scope:CurrentUser -ExecutionPolicy:Bypass

[Handling Notes] Alternative reasons for Configuration system failed to initialize exception

Configuration System failed to initialize exception

There is a c# program on a computer, when performing an operation, it will always report the following error:

Baidu Yi found that the common reason in the market is the location of a specific node in the configuration file is wrong, or the configuration file is damaged, etc., and this program does not use the built-in configuration file scheme, but ini, so it does not apply to my problem. As a result of this program is not mine, so first reflator out source code (c # let I’ve struggled with this, on the one hand, afraid of his own program was against, on the other hand, thanks to the ~ to the others, NND), debug tracking under, found that was WebRequest. The Create () caused by abnormal, this method will most because of the incoming uri string format error caused illegal exception, this can cause the error was the first time to see (I want to Create methods internal is also caused by other classes, but did not explore), anyway, Since it is a non-mainstream exception raised by the built-in class, we try:
Reinstall the.net Framework
Doing weigh in hand.

posted on
The ticket is 2013-07-15
Ahdung reading (
…). Comments (
…).
The editor
collection

Reproduced in: https://www.cnblogs.com/ahdung/p/3191370.html

C# SevenZip simple operation

using SevenZip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Test
{
    public static class ZipHelper
    {
        static bool Is64 = (IntPtr.Size == 8);
        internal const string Name32 = "7z.dll";
        internal const string Name64 = "7z64.dll";
        internal const string ZIP_FILE = "index.zip";

        static ZipHelper()
        {
            string name = Is64 ?Name64 : Name32;
            SevenZip.SevenZipExtractor.SetLibraryPath(name);
        }

        public static Stream GetSteamFrom7z(string fileName)
        {
            var stream = new MemoryStream();
            using (var tmp = new SevenZipExtractor(ZIP_FILE))
            {
                tmp.ExtractFile(fileName, stream);
            }
            stream.Position = 0;
            return stream;
        }
    }
}