Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Last active July 20, 2025 16:46
Show Gist options
  • Save karenpayneoregon/0c4c3f86ddc0021cb46ccfd187afc917 to your computer and use it in GitHub Desktop.
Save karenpayneoregon/0c4c3f86ddc0021cb46ccfd187afc917 to your computer and use it in GitHub Desktop.
Working with ChatGPT to create C# code that ChatGPT writes faster than any developer can

Using C# create a class with a method to read a .csproj file.

  1. Check if UserSecretsId node exists in a method named Exists
  2. If UserSecretsId node exists return the value in a method named GetValue
<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFramework>net9.0</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
		<UserSecretsId>78d698de-c775-468e-b342-5eb6db275a35</UserSecretsId>
	</PropertyGroup>

</Project>

Here's a straightforward C# class that reads a .csproj file and provides two methods:

Exists() — Checks if the node exists.

GetValue() — Returns the value of the if present.

This implementation uses System.Xml.Linq for simplicity and readability.

✅ CsProjSecretsReader.cs

using System;
using System.IO;
using System.Xml.Linq;

public class CsProjSecretsReader
{
    private readonly string _csprojFilePath;
    private readonly XDocument _projectXml;

    public CsProjSecretsReader(string csprojFilePath)
    {
        if (string.IsNullOrWhiteSpace(csprojFilePath))
            throw new ArgumentException("Path to .csproj file must be provided.");

        if (!File.Exists(csprojFilePath))
            throw new FileNotFoundException("The .csproj file was not found.", csprojFilePath);

        _csprojFilePath = csprojFilePath;
        _projectXml = XDocument.Load(_csprojFilePath);
    }

    public bool Exists()
    {
        return GetUserSecretsElement() != null;
    }

    public string? GetValue()
    {
        var element = GetUserSecretsElement();
        return element?.Value;
    }

    private XElement? GetUserSecretsElement()
    {
        // Look for UserSecretsId in any PropertyGroup
        return _projectXml
            .Descendants("PropertyGroup")
            .Elements("UserSecretsId")
            .FirstOrDefault();
    }
}

📌 Usage Example

class Program
{
    static void Main()
    {
        var reader = new CsProjSecretsReader(@"C:\Projects\MyApp\MyApp.csproj");

        if (reader.Exists())
        {
            Console.WriteLine("UserSecretsId found:");
            Console.WriteLine(reader.GetValue());
        }
        else
        {
            Console.WriteLine("UserSecretsId not found.");
        }
    }
}

🛠 Notes This assumes the .csproj file is well-formed XML.

You can expand this to validate GUID format or handle multiple sections if needed.

XDocument.Load throws on malformed XML—consider wrapping it with try/catch if working with unstable inputs.

Would you like a version using XmlDocument for a more traditional approach, or is LINQ to XML your preferred style?

Using ChatGPT

Usually, when I ask ChatGPT to create code, class names are left to ChatGPT, and method names may or may not change.

Inspect

  • Always review and test code.
  • If a class name does not make sense, change the name.
  • If a method name does not easily convey meaning, change the name.
  • If code throws question if it makes sense, if not refactor the code.

Changes made to ChatGPT response

Class name and method names were changed and ProjectSecretsReader constructor changed so not to throw on error. Refactored code is not shown.

Comments are disabled for this gist.