<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nayana Agarwal</title>
    <description>The latest articles on DEV Community by Nayana Agarwal (@nayanaska).</description>
    <link>https://dev.to/nayanaska</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3130099%2F8872ef2e-3d68-4ce4-8e9a-3b03085fe59e.png</url>
      <title>DEV Community: Nayana Agarwal</title>
      <link>https://dev.to/nayanaska</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nayanaska"/>
    <language>en</language>
    <item>
      <title>🐍 Keep Your PC Awake with Python (Perfect for RDP Users!)</title>
      <dc:creator>Nayana Agarwal</dc:creator>
      <pubDate>Tue, 20 May 2025 14:21:41 +0000</pubDate>
      <link>https://dev.to/nayanaska/keep-your-pc-awake-with-python-perfect-for-rdp-users-3cde</link>
      <guid>https://dev.to/nayanaska/keep-your-pc-awake-with-python-perfect-for-rdp-users-3cde</guid>
      <description>&lt;p&gt;Ever been deep into a remote session or running a long task when suddenly… 💥 your system decides to nap?&lt;/p&gt;

&lt;p&gt;If you’re using RDP (Remote Desktop Protocol) — Windows’ built-in way to connect to a remote computer — you know how frustrating it is when your remote machine times out due to inactivity. RDP sessions can drop if Windows thinks you're idle, even though your job is still running.&lt;/p&gt;

&lt;p&gt;Let’s fix that. Here's a Python script that:&lt;br&gt;
✅ Prevents Windows from sleeping&lt;br&gt;
✅ Keeps the display active&lt;br&gt;
✅ Simulates keypresses to fake user activity&lt;br&gt;
✅ Keeps your RDP session alive&lt;/p&gt;

&lt;p&gt;We'll break this script down line by line, so you know exactly what it’s doing.&lt;/p&gt;

&lt;p&gt;📦 Step 1: Import Required Libraries&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;import ctypes
import &lt;span class="nb"&gt;time
&lt;/span&gt;import pyautogui
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ctypes: Calls Windows API functions to control power state.&lt;/p&gt;

&lt;p&gt;time: Adds pauses between keypresses.&lt;/p&gt;

&lt;p&gt;pyautogui: Simulates keyboard/mouse input (as if you're still at the keyboard).&lt;/p&gt;

&lt;p&gt;⚙️ Step 2: Define Windows Power Flags&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ES_CONTINUOUS &lt;span class="o"&gt;=&lt;/span&gt; 0x80000000
ES_AWAYMODE_REQUIRED &lt;span class="o"&gt;=&lt;/span&gt; 0x00000040
ES_SYSTEM_REQUIRED &lt;span class="o"&gt;=&lt;/span&gt; 0x00000001
ES_DISPLAY_REQUIRED &lt;span class="o"&gt;=&lt;/span&gt; 0x00000002

These constants are passed to the Windows API to tell it:

Constant    Meaning
ES_CONTINUOUS   Keep the settings active continuously
ES_AWAYMODE_REQUIRED    Prevent idle &lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;even &lt;span class="k"&gt;if &lt;/span&gt;background&lt;span class="o"&gt;)&lt;/span&gt;
ES_SYSTEM_REQUIRED  Don&lt;span class="s1"&gt;'t let the system sleep
ES_DISPLAY_REQUIRED Keep the screen turned on
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🛑 Step 3: Disable Fail-Safe for Automation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pyautogui.FAILSAFE &lt;span class="o"&gt;=&lt;/span&gt; False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PyAutoGUI stops scripts if the mouse moves to the top-left corner.&lt;/p&gt;

&lt;p&gt;This disables that behavior — useful when running scripts in headless or remote sessions (like RDP).&lt;/p&gt;

&lt;p&gt;💻 Step 4: Prevent Sleep Function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;def prevent_sleep&lt;span class="o"&gt;()&lt;/span&gt;:
    ctypes.windll.kernel32.SetThreadExecutionState&lt;span class="o"&gt;(&lt;/span&gt;
        ES_CONTINUOUS | ES_AWAYMODE_REQUIRED | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED
    &lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function tells Windows:&lt;/p&gt;

&lt;p&gt;“Do not sleep. Keep the display on. Keep the system active — indefinitely.”&lt;/p&gt;

&lt;p&gt;💤 Step 5: Allow Sleep Again (On Exit)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;def allow_sleep&lt;span class="o"&gt;()&lt;/span&gt;:
    ctypes.windll.kernel32.SetThreadExecutionState&lt;span class="o"&gt;(&lt;/span&gt;ES_CONTINUOUS&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Resets the system back to its normal sleep behavior.&lt;br&gt;
We’ll call this when the user stops the script.&lt;/p&gt;

&lt;p&gt;🧑‍💻 Step 6: Keep Your RDP Session Active&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;def keep_rdp_alive&lt;span class="o"&gt;()&lt;/span&gt;:
    print&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Keeping RDP session alive... Press Ctrl+C to stop."&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    prevent_sleep&lt;span class="o"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;while &lt;/span&gt;True:
        pyautogui.press&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'shift'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  &lt;span class="c"&gt;# Simulate a Shift keypress&lt;/span&gt;
        time.sleep&lt;span class="o"&gt;(&lt;/span&gt;240&lt;span class="o"&gt;)&lt;/span&gt;           &lt;span class="c"&gt;# Wait 4 minutes before repeating&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This:&lt;/p&gt;

&lt;p&gt;Calls prevent_sleep() to block system sleep&lt;/p&gt;

&lt;p&gt;Then loops forever, pressing Shift every 4 minutes to trick Windows into thinking you’re still active&lt;/p&gt;

&lt;p&gt;Helps prevent RDP session timeouts (Windows assumes you’re idle if there's no input)&lt;/p&gt;

&lt;p&gt;▶️ Step 7: Run the Script&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;try:
    print&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Preventing system sleep. Press Ctrl+C to stop."&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    keep_rdp_alive&lt;span class="o"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;while &lt;/span&gt;True:
        time.sleep&lt;span class="o"&gt;(&lt;/span&gt;60&lt;span class="o"&gt;)&lt;/span&gt;  &lt;span class="c"&gt;# Keep script running&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Starts the magic and keeps the script alive.&lt;br&gt;
Ctrl+C stops it.&lt;/p&gt;

&lt;p&gt;🧹 Step 8: Clean Exit&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;except KeyboardInterrupt:
    print&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Restoring sleep settings..."&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    allow_sleep&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you hit Ctrl+C, this block catches the interruption and restores sleep settings properly.&lt;/p&gt;

&lt;p&gt;🧠 What Is RDP?&lt;br&gt;
RDP (Remote Desktop Protocol) is a Microsoft protocol that lets you connect to and control a remote Windows machine — as if you were sitting in front of it.&lt;/p&gt;

&lt;p&gt;It’s used by developers, sysadmins, and IT teams to:&lt;/p&gt;

&lt;p&gt;Manage remote servers&lt;/p&gt;

&lt;p&gt;Access desktops from anywhere&lt;/p&gt;

&lt;p&gt;Perform long-running tasks like builds, downloads, etc.&lt;/p&gt;

&lt;p&gt;Problem is, RDP sessions time out if Windows thinks you’re inactive. Even if your script is running, the system may go to sleep or lock the session. That’s why simulating user input is so useful.&lt;/p&gt;

&lt;p&gt;✅ TL;DR: What This Script Does&lt;br&gt;
Prevents system and display sleep via Windows API&lt;/p&gt;

&lt;p&gt;Simulates Shift keypress every 4 minutes&lt;/p&gt;

&lt;p&gt;Keeps RDP sessions alive and stable&lt;/p&gt;

&lt;p&gt;Cleanly exits and restores sleep behavior&lt;/p&gt;

&lt;p&gt;🔧 Want to Customize It?&lt;br&gt;
Adjust the time.sleep(240) interval if your RDP session times out faster.&lt;/p&gt;

&lt;p&gt;Simulate different keys or mouse movements.&lt;/p&gt;

&lt;p&gt;Add a tray icon or system service wrapper if running long-term.&lt;/p&gt;

&lt;p&gt;🧪 Copy the Full Script&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;import ctypes
import &lt;span class="nb"&gt;time
&lt;/span&gt;import pyautogui

&lt;span class="c"&gt;# Prevent sleep (Keeps system &amp;amp; display active)&lt;/span&gt;
ES_CONTINUOUS &lt;span class="o"&gt;=&lt;/span&gt; 0x80000000
ES_AWAYMODE_REQUIRED &lt;span class="o"&gt;=&lt;/span&gt; 0x00000040
ES_SYSTEM_REQUIRED &lt;span class="o"&gt;=&lt;/span&gt; 0x00000001
ES_DISPLAY_REQUIRED &lt;span class="o"&gt;=&lt;/span&gt; 0x00000002
pyautogui.FAILSAFE &lt;span class="o"&gt;=&lt;/span&gt; False

def prevent_sleep&lt;span class="o"&gt;()&lt;/span&gt;:
    ctypes.windll.kernel32.SetThreadExecutionState&lt;span class="o"&gt;(&lt;/span&gt;
        ES_CONTINUOUS | ES_AWAYMODE_REQUIRED | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED
    &lt;span class="o"&gt;)&lt;/span&gt;

def allow_sleep&lt;span class="o"&gt;()&lt;/span&gt;:
    ctypes.windll.kernel32.SetThreadExecutionState&lt;span class="o"&gt;(&lt;/span&gt;ES_CONTINUOUS&lt;span class="o"&gt;)&lt;/span&gt;

def keep_rdp_alive&lt;span class="o"&gt;()&lt;/span&gt;:
    print&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Keeping RDP session alive... Press Ctrl+C to stop."&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    prevent_sleep&lt;span class="o"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;while &lt;/span&gt;True:
        pyautogui.press&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'shift'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        time.sleep&lt;span class="o"&gt;(&lt;/span&gt;240&lt;span class="o"&gt;)&lt;/span&gt;

try:
    print&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Preventing system sleep. Press Ctrl+C to stop."&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    keep_rdp_alive&lt;span class="o"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;while &lt;/span&gt;True:
        time.sleep&lt;span class="o"&gt;(&lt;/span&gt;60&lt;span class="o"&gt;)&lt;/span&gt;
except KeyboardInterrupt:
    print&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Restoring sleep settings..."&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    allow_sleep&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me know if you'd like a version for macOS/Linux or a version that includes a GUI!&lt;/p&gt;

</description>
      <category>pythonautomation</category>
      <category>rdp</category>
      <category>remotedesktop</category>
      <category>windowshacks</category>
    </item>
    <item>
      <title>🚀 Roslyn UnUsed Code Remover – Clean Up Your .NET Projects with Ease</title>
      <dc:creator>Nayana Agarwal</dc:creator>
      <pubDate>Wed, 07 May 2025 10:41:53 +0000</pubDate>
      <link>https://dev.to/nayanaska/roslyn-unused-code-remover-clean-up-your-net-projects-with-ease-355j</link>
      <guid>https://dev.to/nayanaska/roslyn-unused-code-remover-clean-up-your-net-projects-with-ease-355j</guid>
      <description>&lt;p&gt;Have you ever returned to a .NET project only to find it cluttered with unused code—methods, variables, or even entire classes that serve no purpose anymore? If so, you're not alone. As projects grow and evolve, it's easy for dead code to accumulate, quietly bloating your codebase and making maintenance harder.&lt;/p&gt;

&lt;p&gt;That's why I built &lt;strong&gt;&lt;a href="https://github.com/Nayanaska/Roslyn-Unused-Code-Remover" rel="noopener noreferrer"&gt;Roslyn Unused Code Remover&lt;/a&gt;&lt;/strong&gt;—a lightweight CLI tool that automatically identifies and removes unused code from your C# projects using the Roslyn compiler platform.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✨ What It Does
&lt;/h2&gt;

&lt;p&gt;This tool leverages the power of &lt;strong&gt;Roslyn analyzers&lt;/strong&gt; to detect unused code elements in &lt;code&gt;.cs&lt;/code&gt; files and provides a quick way to clean them up. It's especially helpful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Refactoring old codebases&lt;/li&gt;
&lt;li&gt;Removing noise before code reviews&lt;/li&gt;
&lt;li&gt;Keeping things lean and maintainable&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⚙️ How It Works
&lt;/h2&gt;

&lt;p&gt;Under the hood, the tool:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Parses the solution or project using Roslyn APIs.&lt;/li&gt;
&lt;li&gt;Analyzes syntax trees and semantic models to find symbols (variables,function and classes(whose all methods have 0 reference).) that are declared but never referenced.&lt;/li&gt;
&lt;li&gt;Comments or flags them for your review which you can later search in folder using "// Remove unused code" .&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's like having an assistant that knows your code well enough to do spring cleaning for you.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠️ Getting Started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Clone the Repo
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/Nayanaska/Roslyn-Unused-Code-Remover.git
&lt;span class="nb"&gt;cd &lt;/span&gt;Roslyn-Unused-Code-Remover
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Build and Run&lt;br&gt;
Make sure you have the .NET SDK installed. Then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet build
dotnet run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to Program.cs file and put path of your solution, the path of log files to check all unused code references that is found and excel file where you want to list down all unused class references under variables solutionPath, LogFilePath and unusedClassesExcelPath and run using command,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tool will analyze your code and comment and flag unused code blocks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code explanation
&lt;/h3&gt;

&lt;p&gt;📌 Main Method – The Entry Point&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;static async Task Main&lt;span class="o"&gt;(&lt;/span&gt;string[] args&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where the program starts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;MSBuildLocator.RegisterDefaults&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Registers MSBuild so Roslyn can load projects and solutions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;string solutionPath &lt;span class="o"&gt;=&lt;/span&gt; @&lt;span class="s2"&gt;"Path/to/your/solution"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sets the path to the .sln file you want to analyze.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; static &lt;span class="nb"&gt;readonly &lt;/span&gt;string unusedClassesExcelPath &lt;span class="o"&gt;=&lt;/span&gt; @&lt;span class="s2"&gt;"path&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;o&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="s2"&gt;ave&lt;/span&gt;&lt;span class="se"&gt;\u&lt;/span&gt;&lt;span class="s2"&gt;nusedclassesexcelfile.xlsx"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sets the path to the .xlsx file you want to save the location of all unused classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;static &lt;span class="nb"&gt;readonly &lt;/span&gt;string LogFilePath &lt;span class="o"&gt;=&lt;/span&gt; @&lt;span class="s2"&gt;"path/of/log/file.txt"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sets the path to the .txt file you want to save logs in.&lt;/p&gt;

&lt;p&gt;🚫 Ignored Folders&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;static &lt;span class="nb"&gt;readonly &lt;/span&gt;List&amp;lt;string&amp;gt; IgnoredFolders &lt;span class="o"&gt;=&lt;/span&gt; new List&amp;lt;string&amp;gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"Folder1"&lt;/span&gt;,
    &lt;span class="s2"&gt;"folder2"&lt;/span&gt;,
    &lt;span class="s2"&gt;"folder3"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Prevents cleaning test or sample code where unused items may be expected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;using StreamWriter logWriter &lt;span class="o"&gt;=&lt;/span&gt; new&lt;span class="o"&gt;(&lt;/span&gt;LogFilePath, append: &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
Console.SetOut&lt;span class="o"&gt;(&lt;/span&gt;logWriter&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
Console.SetError&lt;span class="o"&gt;(&lt;/span&gt;logWriter&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Redirects all logs and errors to a file for easy review.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;using var workspace &lt;span class="o"&gt;=&lt;/span&gt; MSBuildWorkspace.Create&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
var solution &lt;span class="o"&gt;=&lt;/span&gt; await workspace.OpenSolutionAsync&lt;span class="o"&gt;(&lt;/span&gt;solutionPath&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creates a Roslyn workspace and loads your solution into it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var unusedVariables &lt;span class="o"&gt;=&lt;/span&gt; await FindUnusedVariables&lt;span class="o"&gt;(&lt;/span&gt;solution&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
await CommentOutUnusedVariables&lt;span class="o"&gt;(&lt;/span&gt;unusedVariables&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Detects and comments out unused variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var unusedFunctions &lt;span class="o"&gt;=&lt;/span&gt; await FindUnusedFunctions&lt;span class="o"&gt;(&lt;/span&gt;solutionPath&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
await CommentOutUnusedFunctions&lt;span class="o"&gt;(&lt;/span&gt;unusedFunctions&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same process for methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var unusedClasses &lt;span class="o"&gt;=&lt;/span&gt; await FindUnusedClasses&lt;span class="o"&gt;(&lt;/span&gt;solutionPath&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
await CommentOutUnusedClasses&lt;span class="o"&gt;(&lt;/span&gt;unusedClasses&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And for classes.&lt;/p&gt;

&lt;p&gt;🔍 FindUnusedVariables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var variables &lt;span class="o"&gt;=&lt;/span&gt; root.DescendantNodes&lt;span class="o"&gt;()&lt;/span&gt;
    .OfType&amp;lt;VariableDeclaratorSyntax&amp;gt;&lt;span class="o"&gt;()&lt;/span&gt;
    .Select&lt;span class="o"&gt;(&lt;/span&gt;v &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; new &lt;span class="o"&gt;{&lt;/span&gt; Symbol &lt;span class="o"&gt;=&lt;/span&gt; semanticModel.GetDeclaredSymbol&lt;span class="o"&gt;(&lt;/span&gt;v&lt;span class="o"&gt;)&lt;/span&gt;, SyntaxNode &lt;span class="o"&gt;=&lt;/span&gt; v &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finds all variable declarations and fetches their symbols.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var references &lt;span class="o"&gt;=&lt;/span&gt; await SymbolFinder.FindReferencesAsync&lt;span class="o"&gt;(&lt;/span&gt;variable.Symbol, solution&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Checks where each variable is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;references.Any&lt;span class="o"&gt;(&lt;/span&gt;refs &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; refs.Locations.Any&lt;span class="o"&gt;()))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there are no references, it’s considered unused.&lt;/p&gt;

&lt;p&gt;🧠 FindUnusedFunctions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var methods &lt;span class="o"&gt;=&lt;/span&gt; root.DescendantNodes&lt;span class="o"&gt;()&lt;/span&gt;.OfType&amp;lt;MethodDeclarationSyntax&amp;gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Extracts all method declarations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var symbol &lt;span class="o"&gt;=&lt;/span&gt; semanticModel.GetDeclaredSymbol&lt;span class="o"&gt;(&lt;/span&gt;method&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Gets the Roslyn symbol for each method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var refs &lt;span class="o"&gt;=&lt;/span&gt; await SymbolFinder.FindReferencesAsync&lt;span class="o"&gt;(&lt;/span&gt;symbol, solution&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Checks if the method is ever called. If not—💥 it's unused!&lt;/p&gt;

&lt;p&gt;✂️ CommentOutUnusedVariables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var unusedItems &lt;span class="o"&gt;=&lt;/span&gt; unusedVariables.GroupBy&lt;span class="o"&gt;(&lt;/span&gt;item &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; item.filePath&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Groups unused variables by their file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;lines[item.lineNumber - 1] &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"// Remove unused code"&lt;/span&gt; + lines[item.lineNumber - 1]&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comments out the variable declaration directly in the source file.&lt;/p&gt;

&lt;p&gt;🔧 CommentOutUnusedFunctions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var methodNode &lt;span class="o"&gt;=&lt;/span&gt; root.DescendantNodes&lt;span class="o"&gt;()&lt;/span&gt;
    .OfType&amp;lt;MethodDeclarationSyntax&amp;gt;&lt;span class="o"&gt;()&lt;/span&gt;
    .FirstOrDefault&lt;span class="o"&gt;(&lt;/span&gt;n &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; syntaxTree.GetLineSpan&lt;span class="o"&gt;(&lt;/span&gt;n.Span&lt;span class="o"&gt;)&lt;/span&gt;.StartLinePosition.Line &lt;span class="o"&gt;==&lt;/span&gt; item.lineNumber - 1&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finds the method node based on its line number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;int i &lt;span class="o"&gt;=&lt;/span&gt; startLine&lt;span class="p"&gt;;&lt;/span&gt; i &amp;lt;&lt;span class="o"&gt;=&lt;/span&gt; endLine&lt;span class="p"&gt;;&lt;/span&gt; i++&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    updatedLines[i] &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"// "&lt;/span&gt; + updatedLines[i]&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comments out the entire method.&lt;/p&gt;

&lt;p&gt;🧹 FindUnusedClasses&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var classSymbol &lt;span class="o"&gt;=&lt;/span&gt; semanticModel.GetDeclaredSymbol&lt;span class="o"&gt;(&lt;/span&gt;classDecl&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fetches the class symbol.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var classRefs &lt;span class="o"&gt;=&lt;/span&gt; await SymbolFinder.FindReferencesAsync&lt;span class="o"&gt;(&lt;/span&gt;classSymbol, solution&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Checks for references to the class itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;foreach &lt;span class="o"&gt;(&lt;/span&gt;var member &lt;span class="k"&gt;in &lt;/span&gt;classSymbol.GetMembers&lt;span class="o"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also checks if any member of the class (methods, properties, etc.) is used.&lt;/p&gt;

&lt;p&gt;🪓 CommentOutUnusedClasses&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;var classNode &lt;span class="o"&gt;=&lt;/span&gt; root.DescendantNodes&lt;span class="o"&gt;()&lt;/span&gt;
    .OfType&amp;lt;ClassDeclarationSyntax&amp;gt;&lt;span class="o"&gt;()&lt;/span&gt;
    .FirstOrDefault&lt;span class="o"&gt;(&lt;/span&gt;n &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; ...&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finds the class node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;updatedLines.Insert&lt;span class="o"&gt;(&lt;/span&gt;startLine, &lt;span class="s2"&gt;"// Remove unused code"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adds a comment before the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;int i &lt;span class="o"&gt;=&lt;/span&gt; startLine + 1&lt;span class="p"&gt;;&lt;/span&gt; i &amp;lt;&lt;span class="o"&gt;=&lt;/span&gt; endLine&lt;span class="p"&gt;;&lt;/span&gt; i++&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    updatedLines[i] &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"// "&lt;/span&gt; + updatedLines[i]&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comments out the entire class block.&lt;/p&gt;

&lt;p&gt;🛠️ BuildSolution&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ProcessStartInfo psi &lt;span class="o"&gt;=&lt;/span&gt; new&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"dotnet"&lt;/span&gt;, &lt;span class="s2"&gt;$"build &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;{solutionPath}&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Runs dotnet build to ensure the solution is still valid.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;return &lt;/span&gt;process.ExitCode &lt;span class="o"&gt;==&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returns true if build succeeded, false otherwise.&lt;/p&gt;

&lt;p&gt;📤 SaveFailedChangesToExcel &amp;amp; SaveUnusedClassesToExcel&lt;br&gt;
These use EPPlus to export results to Excel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;using var package &lt;span class="o"&gt;=&lt;/span&gt; new ExcelPackage&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
var worksheet &lt;span class="o"&gt;=&lt;/span&gt; package.Workbook.Worksheets.Add&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Unused Classes"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creates an Excel file and writes data like:&lt;/p&gt;

&lt;p&gt;File path&lt;/p&gt;

&lt;p&gt;Line number&lt;/p&gt;

&lt;p&gt;Code snippet&lt;/p&gt;

&lt;p&gt;Build status&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Why I Built It&lt;/strong&gt;&lt;br&gt;
During my development work, I often found myself manually hunting for unused code. I realized this was both time-consuming and error-prone. While tools like ReSharper offer some of this functionality, I wanted a free, scriptable, and open-source solution I could use in CI pipelines or one-off cleanup tasks.&lt;/p&gt;

&lt;p&gt;🧪 &lt;strong&gt;Limitations &amp;amp; Future Plans&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Doesn't yet handle dynamically-referenced code or reflection-heavy patterns.&lt;/p&gt;

&lt;p&gt;Plans include:&lt;/p&gt;

&lt;p&gt;Optional "preview mode"&lt;/p&gt;

&lt;p&gt;VS Code integration&lt;/p&gt;

&lt;p&gt;CI-friendly reporting output&lt;/p&gt;

&lt;p&gt;🙌 &lt;strong&gt;Contributions Welcome&lt;/strong&gt;&lt;br&gt;
If you're passionate about Roslyn or static code analysis, feel free to open issues or submit PRs. I'd love to collaborate with the community on making this tool smarter and more robust.&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;&lt;a href="https://github.com/Nayanaska/Roslyn-Unused-Code-Remover" rel="noopener noreferrer"&gt;Check out the repo&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;🔚 Final Thoughts&lt;br&gt;
Code cleanliness is more than a style preference—it's a productivity booster. With Roslyn UnUsed Code Remover, you can keep your C# projects sharp, focused, and free of digital clutter. Give it a try and let me know how it works for you!&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>roslyn</category>
      <category>unusedcoderemover</category>
      <category>cli</category>
    </item>
  </channel>
</rss>
