Added code not executed as expected

Jonathan 500 Reputation points
2026-12-04T09:06:54.4066667+00:00

Hi,

I added the part (of code) below into the project and cannot get the expected result. Is it allowed that I uploaded that project into Github for you to help?

User's image

Developer technologies | C#
Developer technologies | 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.


2 answers

Sort by: Most helpful
  1. Harry Nguyen (WICLOUD CORPORATION) 1,025 Reputation points Microsoft External Staff Moderator
    2026-12-04T09:47:18.1033333+00:00

    Hi @Jonathan , 

    From the screenshot, it looks like your code is extracting a field from a string, writing it to a file, and then comparing it with a previous value (Unb_last_fie). 

    For example, suppose the input is: 

    • Line0 = "UNZ+1+ABC123'" 
    • Unb_last_fie = "XYZ789" 

    If the code successfully extracts ABC123 as Unz_fie, it should write that value to the file. Since XYZ789 and ABC123 are different, the subsequent validation block should also execute. 

    Based on this behavior, here are a few things you can check: 

    1. First, check the values of Pos0, Pos1, and Pos2 to make sure the delimiters are found at the expected positions. If IndexOf() returns -1, the subsequent Substring() operation may fail or produce an unexpected result. 
    2. Also, keep in mind that the second block only runs when both Unb_last_fie and Unz_fie are non-empty and their values are different. Otherwise, that block will be skipped. 
    3. Finally, check the value of allowappend, as it determines whether StreamWriter appends content to the existing file or overwrites it. 

    You can try checking these values in the debugger to see where the actual behavior differs from what you expect. 

    If the issue persists, feel free to upload a minimal reproducible version of your project to a public GitHub repository and share the link here. Please remove any sensitive information before uploading, and include a brief description of what the code is intended to do, along with the expected and actual results. This will help me take a closer look at the issue. 

    I hope these suggestions help you narrow down the issue. If you find the information helpful, I'd appreciate it if you could refer to the guide for your confirmation. 

    Thank you. 

    Was this answer helpful?


  2. AI answer

    2026-12-04T09:07:41.8233333+00:00

    Yes, upload a sanitized, minimal version of the C# project to a publicly accessible GitHub repository and add its link to the question.

    Before uploading

    Remove or replace:

    • Passwords, API keys, tokens, connection strings, and certificates.
    • Personal, customer, or production data.
    • Tenant, subscription, account, server, and internal network identifiers.
    • Build artifacts and IDE-specific folders such as bin, obj, and .vs.

    Use placeholder values such as [Email] and include a .gitignore appropriate for Visual Studio/.NET.

    If a secret has already been committed, deleting it from the latest file is insufficient because it can remain in Git history. Revoke or rotate it immediately; history can then be cleaned if necessary.^1^

    Make the repository reproducible

    Prefer a small project containing only the code required to reproduce the behavior. Include a README with:

    1. The .NET and C# versions.
    2. The project type and IDE/version.
    3. Build and run commands.
    4. A sample value for Line0.
    5. The expected file output.
    6. The actual file output or exception and complete stack trace.
    7. The exact steps that trigger the problem.

    For example:

    Environment: .NET 8, C# 12, Windows 11
    Run: dotnet restore
         dotnet run
    
    Input Line0: <sanitized sample>
    Expected: <expected File1 contents>
    Actual: <actual File1 contents or exception>
    

    Microsoft Q&A recommends supplying the environment, expected and actual results, errors, troubleshooting performed, and a minimal reproducible test case.^2^

    Information missing from the screenshot

    The fragment alone is not enough to determine why the result differs. In particular, include:

    • Representative values of Line0, Unb_last_fie, File1, and allowappend.
    • The values returned for Pos0, Pos1, and Pos2.
    • Whether the code reaches each using block.
    • Whether File1 already exists and whether append mode is enabled.
    • Any exception shown by the debugger.

    Because the code uses IndexOf results as Substring indexes, also capture those positions before extraction:

    Console.WriteLine($"Line0: {Line0}");
    Console.WriteLine($"Pos0={Pos0}, Pos1={Pos1}, Pos2={Pos2}");
    Console.WriteLine($"Unb_last_fie='{Unb_last_fie}'");
    Console.WriteLine($"Unz_fie='{Unz_fie}'");
    Console.WriteLine($"File1='{File1}', append={allowappend}");
    

    If any IndexOf call returns -1, provide the corresponding input rather than masking the result; it will show which expected delimiter was not found.

    Post the repository link together with these details rather than only the screenshot. Do not grant strangers access to a private repository or publish proprietary source solely for debugging. Clear, concise reproduction steps and the correct technology tag improve the likelihood of a useful answer.^3^


    References

    1. Removing sensitive data from a repository - GitHub Docs
    2. Ask a question on Microsoft Q&A
    3. Guidelines for posting a quality question on Microsoft Q&A
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.