Advertisement
Inksaver

Launch SharpDevelop on USB or shared drive

Oct 5th, 2016
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.41 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Threading;
  6.  
  7. namespace SharpDevelop
  8. {
  9.     class Program
  10.     {
  11.         public static void Main()
  12.         {
  13.             /*
  14.             For use on portable installations of SharpDevelop on USB or student documents or school shared network drive.
  15.             It changes the setting for the 'New Project' location then launches \bin\SharpDevelop.exe.
  16.             It uses the PortableApps structure, where the SharpDevelop folder is inside PortableApps folder,
  17.             and the .exe file is expected within this folder in order for PortableApps to start it.
  18.             On a USB the project save path is in X:\Documents\Projects\CSharp
  19.             On student documents the save path is <mapped drive>:\Documents\Projects\CSharp (if .exe allowed)
  20.             On a shared drive at schools the save path is <mapped drive>:\Users\<username>\CSharp. (.exe allowed on this drive only)
  21.             This allows multiple users to use the same install, but save their projects under their own username.
  22.            
  23.             Place the .exe generated from this code at the root of the SharpDevelop folder
  24.             This project is called 'SharpDevelop' and located in a sub-folder of the 'SharpDevelop'
  25.             install folder, called 'SharpDevelopPortableLauncher'.
  26.             When compiled it creates an .exe file called SharpDevelop.exe in
  27.             SharpDevelopPortableLauncher\SharpDevelop\bin\Debug\SharpDevelop.exe
  28.             This file should be copied to the root folder of the SharpDevelop installation folder:
  29.             <folder>\SharpDevelop\SharpDevelop.exe
  30.            
  31.             Assume location of this exe is either in same folder as SharpDevelop in PortableApps
  32.             IDE:        <folder>\PortableApps\SharpDevelop\SharpDevelopPortableLauncher\SharpDevelop\bin\Debug\SharpDevelop.exe
  33.             Compiled:   <folder>\PortableApps\SharpDevelop\SharpDevelop.exe
  34.             --> either one launches <folder>\PortableApps\SharpDevelop\bin\SharpDevelop.exe to run the IDE       
  35.             need to adjust SharpDevelop\Settings\SharpDevelopProperties.xml for 'NewProjectDialog'
  36.             SharpDevelop could be in:
  37.             1-<any folder or root of drive>\SharpDevelop - Do not alter the settings!
  38.             2-<folder or drive>\PortableApps\SharpDevelop with <folder or drive>\Documents
  39.              = on USB or User Profile on school system. Set save location to \Documents\Projects\CSharp
  40.             3-<folder or drive>\PortableApps\SharpDevelop with <folder or drive>\Users
  41.              = on shared drive. Set save location to \Users\UserName\CSharp
  42.             */
  43.             Console.WriteLine("PortableApps Launcher for SharpDevelop");
  44.             string driveLetter = Path.GetPathRoot(Environment.CurrentDirectory); // X:\
  45.             Console.WriteLine("Drive Letter: " + driveLetter);
  46.             //full startup of .exe eg in .bin folder
  47.             string AppPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  48.             Console.WriteLine("App Path: " + AppPath);     
  49.             string SDPath = AppPath.Substring(0, AppPath.IndexOf(@"\SharpDevelop")) + @"\SharpDevelop"; // e.g W:\ICT-CLUB\PortableApps\
  50.             Console.WriteLine("SharpDevelop.exe (launcher) located at: " + SDPath);
  51.             Console.WriteLine("SharpDevelop.exe (executable) located at: " + SDPath + @"\bin\SharpDevelop.exe");
  52.             string RootPath = string.Empty;
  53.             string SavesFolder = string.Empty;     
  54.             // Check for \PortableApps eg W:\ICT-CLUB\PortableApps\
  55.             if(AppPath.Contains("PortableApps")) //inside PortableApps, so change settings
  56.             {
  57.                 RootPath = AppPath.Substring(0, AppPath.IndexOf(@"\PortableApps") + 1); // X:\, W:\ICT-CLUB\
  58.                 Console.WriteLine("RootPath = " + RootPath);
  59.                 // Check for \Documents
  60.                 if(Directory.Exists(Path.Combine(RootPath, @"Documents"))) //on USB OR school user space. Create CSharp folder (No effect if exists)
  61.                 {
  62.                     //if on usb RootPath = driveLetter
  63.                     if(RootPath == driveLetter) // on USB
  64.                     {
  65.                         Console.WriteLine("Running from USB (" + driveLetter + ")");                       
  66.                     }
  67.                     else // not on usb: student space
  68.                     {
  69.                         Console.WriteLine("Running from Documents: (" + RootPath + ")");
  70.                     }
  71.                     if(Directory.Exists(Path.Combine(RootPath, @"Documents\Projects")))     // Documents\Projects already exists
  72.                     {
  73.                         if(!Directory.Exists(Path.Combine(RootPath, @"Documents\Projects\CSharp")))
  74.                         {
  75.                             Console.WriteLine("Creating saves folders");
  76.                             Directory.CreateDirectory(Path.Combine(RootPath, @"\Documents\Projects\CSharp"));
  77.                         }
  78.                         SavesFolder = Path.Combine(RootPath, @"Documents\Projects\CSharp");
  79.                     }
  80.                     else if(Directory.Exists(Path.Combine(RootPath, @"Documents\CSharp")))  // Documents\Projects\CSharp already exists
  81.                     {
  82.                         SavesFolder = Path.Combine(RootPath, @"Documents\CSharp");
  83.                     }
  84.                     else
  85.                     {
  86.                         SavesFolder = Path.Combine(RootPath, @"Documents\Projects\CSharp");
  87.                         Console.WriteLine("Creating / checking saves folders");
  88.                         Directory.CreateDirectory(Path.Combine(RootPath, @"\Documents\Projects\"));
  89.                         Directory.CreateDirectory(Path.Combine(RootPath, @"\Documents\Projects\CSharp"));
  90.                     }  
  91.                 }
  92.                 else if(Directory.Exists(Path.Combine(RootPath, @"Users"))) // on shared folder: create folder from username
  93.                 {
  94.                     //Create saves folder based on username for school shared drives
  95.                     Console.WriteLine("Creating / checking saves folders");
  96.                     // CreateDirectory is ignored if the folder already exists
  97.                     Directory.CreateDirectory(Path.Combine(RootPath, @"Users\" + Environment.UserName));
  98.                     Directory.CreateDirectory(Path.Combine(RootPath, @"Users\" + Environment.UserName + @"\CSharp"));
  99.                     //Can also create folders for portable versions of other IDEs
  100.                     Directory.CreateDirectory(Path.Combine(RootPath, @"Users\" + Environment.UserName + @"\HTML"));
  101.                     Directory.CreateDirectory(Path.Combine(RootPath, @"Users\" + Environment.UserName + @"\Lua"));
  102.                     Directory.CreateDirectory(Path.Combine(RootPath, @"Users\" + Environment.UserName + @"\Java"));
  103.                     Directory.CreateDirectory(Path.Combine(RootPath, @"Users\" + Environment.UserName + @"\Python"));
  104.                     Directory.CreateDirectory(Path.Combine(RootPath, @"Users\" + Environment.UserName + @"\Scratch"));
  105.                     SavesFolder = Path.Combine(RootPath, @"Users\" + Environment.UserName + @"\CSharp");
  106.                 }
  107.                 Console.WriteLine("Saves Folder: " + SavesFolder);                                         
  108.                 // now alter settings
  109.                 string settingsFilePath = Path.Combine(SDPath, @"Settings\SharpDevelopProperties.xml");
  110.                 XmlDocument SharpDevelopProperties = new XmlDocument();
  111.                 SharpDevelopProperties.Load(settingsFilePath);
  112.                 try
  113.                 {      
  114.                     XmlElement DefaultPath =
  115.                         (XmlElement)SharpDevelopProperties.SelectSingleNode(
  116.                         "//Properties/Property[@key='ICSharpCode.SharpDevelop.Gui.Dialogs.NewProjectDialog.DefaultPath']");
  117.                     if (DefaultPath != null)
  118.                     {
  119.                         DefaultPath.InnerText = SavesFolder;
  120.                     }      
  121.                     SharpDevelopProperties.Save(settingsFilePath);
  122.                 }
  123.                 catch (Exception e)
  124.                 {
  125.                     Console.WriteLine(e);
  126.                 }  
  127.             }
  128.             else
  129.             {
  130.                 RootPath = AppPath;
  131.                 Console.WriteLine("RootPath = " + RootPath);
  132.             }                                  
  133.             Console.WriteLine("Launching SharpDevelop\n\nHold tight!");
  134.             try
  135.             {
  136.                 System.Diagnostics.Process.Start(Path.Combine(SDPath, @"bin\SharpDevelop.exe"));
  137.                 Console.WriteLine("\nClosing in 30 seconds...");
  138.                 Thread.Sleep(30000);
  139.             }
  140.             catch (Exception e)
  141.             {
  142.                 Console.WriteLine(e.Message);
  143.                 Console.WriteLine("Press enter to exit");
  144.                 Console.ReadLine();
  145.             }
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement