Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

Toggle Duplicate and Extend Monitors Using DisplaySwitch.exe

Description
This script will use Windows' displayswitch.exe to toggle between cloned and extended monitors.
Language
C#.net
Minimum Version
Created By
Dylan24578
Contributors
-
Date Created
Jan 8, 2020
Date Last Modified
Jan 8, 2020

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.IO;

public static class DisplayFusionFunction
	{
		public static void Run()
			{
                //Read/Write file Variables, used to determine current monitor states
                string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); //Appdata Roaming folder
                string folder = @"\displayfusionscripts\"; //scripts folder
                string folderPath = appdata + folder;
                string path = appdata + folder + "data.txt"; //full file path
                
                //First time setup, create directory and data.txt in appdata
                if(!File.Exists(path)){
                    createFile(folderPath, path);
                }
                
                //Read file
                StreamReader sr = new StreamReader(path);
                string data = sr.ReadLine();
                sr.Close();
                
                string c = "/clone";
                string e = "/extend";
                //Check data, change data.txt for next use                
                if(data == c) {
                    writeToFile(path, e);
                } else if (data == e) {
                    writeToFile(path, c);
                } else {
                    writeToFile(path, c);
                }
                
                //run built-in Windows DisplaySwitch.exe with the /extend
                BFS.Application.Start("DisplaySwitch.exe", data);
			}

        //Creates directory and file if they don't exist
        static void createFile(string folderPath, string path){
            Directory.CreateDirectory(folderPath);
            File.Create(path);
        }
        
        
        //Writes data to file
        static void writeToFile(string path, string data) {
            try{
                StreamWriter sw = new StreamWriter(path);
                sw.WriteLine(data);
                sw.Close();
            }  
            catch(Exception e){
                Console.WriteLine(e.Message);
                
            }
        }
    }