I'm using the Typewriter extension for Visual Studio and Visual Studio 2017. The following is the Typescript Template file.
${
using System.IO;
Template(Settings settings)
{
settings.IncludeProject("ProjectName");
}
string Test(Class c)
{
//return Path.GetDirectoryName(Path.GetDirectoryName( System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ))); // > file:\C:\Users\[user]\AppData\Local
//return Environment.CurrentDirectory; // > C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE
return String.Join(", ", Directory.GetFiles("relative\path")); // > System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\relative\path'.
}
}
$Classes(ProjectName.Models.*)[
export class $Name {
$Properties[
public $Name: $Type;]
// $Test
}]
When using Directory.GetFiles the path is relative to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE not relative to the template file or the project. How can I get relative files?
P.S. Not sure if this is specific to Typewriter or Visual Studio Extensions in general.
Edit
I have since tried DTE:
using EnvDTE;
DTE dte = (DTE)GetService(typeof(DTE));
string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);
But I get The type or namespace name 'EnvDTE' could not be found (are you missing a using directive or an assembly reference?) and i don't how to reference an assembly within an existing extension. If I could use DTE that would resolve the initial question.
The $Parent property of the class is a $File and this has a $FullName property which is the absolute file name. I am using this to get the filenames of the interface:
string FileName(Interface #interface)
{
return (#interface.Parent as File).FullName;
}
Maybe from there you can figure out the path to the template file?
Thanks to the comments above I was able to pull this off with the following:
#reference EnvDTE
#reference Microsoft.VisualStudio.Shell.15.0
${
Template(Settings settings)
{
EnvDTE.DTE dte = (EnvDTE.DTE)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE));
var fileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(dte.Solution.FullName), "myfile.txt");
}
}
Related
In Visual Studio, I would like to update the fullprop snipped to automatically convert the field name according to property name: (and so only enter "property name" & "type")
public string MyTest
{
get => _myTest;
set
{
if (_myTest == value) return;
_myTest = value;
RaisePropertyChanged();
}
}
private string _myTest;
I can't find a way to convert MyProperty in myProperty.
I found this topic which seems to work with VSCode but it doesn't work in Visual Studio. Snippet in VSCode seems to be different from Visual Studio as $1$ works in visual studio but ${1} doesn't work. So is there an equivalent way in Visual Studio ?
Thanks for your help !
I work with large html files that I would like to fragment into seperate files. The process of doing this is quite tedious as it requires copying the code, creating a new file, pasting it in the new file, and then selecting a folder and a name to save it under.
Is there a built-in shortcut or extension in Visual Studio 2017 and higher for making this easier?
You can automate it in Visual Studio with my Visual Commander extension. Select the code and call the following command (C# language):
public class C : VisualCommanderExt.ICommand
{
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
string currentFileName = DTE.ActiveDocument.FullName;
string newFileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(currentFileName), System.IO.Path.GetFileNameWithoutExtension(currentFileName) + "NewPart" + System.IO.Path.GetExtension(currentFileName));
EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
System.IO.File.WriteAllText(newFileName, ts.Text);
}
}
I'm trying to use the Windows.System namespace, part of WinRT, in PowerShell.
Instead of a dll, the assembly comes as a winmd. The two ways of loading assemblies into PowerShell don't seem to work
#[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral\Windows.winmd")
#Add-Type -Path "C:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral\Windows.winmd"
I am aware that using Windows API's is a bit trickier than loading my .NET code. I have pinched code for using parts of the win32, would a WinRT solution be similar?
$script:nativeMethods = #();
function Register-NativeMethod([string]$dll, [string]$methodSignature)
{
$script:nativeMethods += [PSCustomObject]#{ Dll = $dll; Signature = $methodSignature; }
}
function Add-NativeMethods()
{
$nativeMethodsCode = $script:nativeMethods | % { "
[DllImport(`"$($_.Dll)`")]
public static extern $($_.Signature);
" }
Add-Type #"
using System;
using System.Runtime.InteropServices;
public static class NativeMethods {
$nativeMethodsCode
}
"#
}
Register-NativeMethod "user32.dll" "int MessageBox (IntPtr hWnd, string text, string caption,int type)"
Add-NativeMethods
[NativeMethods]::MessageBox(0, "Please do not press this again.", "Attention", 0)| Out-Null
My goal is to run Windows.System.Launcher.LaunchFileAsync("C:\file"); in PowerShell. How can I load the WinRT components I need?
To load WinRT binaries into PowerShell use this example:
> [Windows.Data.Json.JsonValue,Windows.Web,ContentType=WindowsRuntime]
> $value = new-object Windows.Data.Json.JsonObject
> $value.Stringify()
{}
I think you will need a StorageFile:
> [Windows.System.Launcher,Windows.Web,ContentType=WindowsRuntime]
> [Windows.System.Launcher]::LaunchFileAsync
OverloadDefinitions
-------------------
static Windows.Foundation.IAsyncOperation[bool] LaunchFileAsync(Windows.Storage.IStorageFile file)
static Windows.Foundation.IAsyncOperation[bool] LaunchFileAsync(Windows.Storage.IStorageFile file, Windows.System.LauncherOptions options)
To create one, you may do something like this:
> [Windows.Management.Core.ApplicationDataManager,Windows.Web,ContentType=WindowsRuntime]
> $value = [Windows.Management.Core.ApplicationDataManager]::CreateForPackageFamily("BackgroundTaskApp_mxmz85hp10cp4")
> $asyncInfo = $value.LocalFolder.CreateFileAsync("foo.txt")
To await *Async() methods, you will need to do something like to create a wrapper.
I wanted to get the full file path when the caret in visual studio is at object Creation or referring a method of some other class.
Something like
Class CurrentClass
{
Class2 object1=new Class2();
object1.method1();
}
Can I get the complete file path like c:\ProjectLocation\Class2.cs.
When I get this line in visual studio.
Class2 object1=new Class2();
You can resolve the active expression (object creation expression, type reference expression, method reference expression), and get the file name with resolved declaration, using code like this:
Expression activeExpression = CodeRush.Source.Active as Expression;
if (activeExpression!= null)
{
IElement declaration = activeExpression.Resolve(new SourceTreeResolver());
if (declaration != null)
{
string fileName = declaration.FirstFile.Name;
// use the fileName...
}
}
I have created my first dll using c# and visual studio 2010. I am trying to use it in a program. The dll is in the program directory but visual studio will not allow using myDLL stating that it could not be found. I have also tried adding it as a reference in the solution explorer. What more do I need to do?
Here is one of the files from my class.
namespace nbt
{
class TAG_Long
{
private string name;
private long payload;
public TAG_Long(FileStream f)
{
name = NameTag.SetTagName(f);
byte[] buffer = new byte[(int)dataBytes.TYPE_LONG];
f.Read(buffer, 0, (int)dataBytes.TYPE_LONG);
Array.Reverse(buffer);
payload = BitConverter.ToInt64(buffer, 0);
}
}
}
Try putting your class in a namespace