I'd like to write a macro to crawl through the files in my project directory and find files that aren't included in the project.
In playing around with the DTE object, I see that the Project object has ProjectItems; if a ProjectItem represents a directory, then it has its own ProjectItems collection. This gives me all files that are included in the project.
So I could crawl recursively through each ProjectItems collection, and for each ProjectItem that's a directory, check to see if there are files in the file system that don't have a corresponding ProjectItem. This seems clumsy, though.
Any ideas of a simpler way to approach this?
Here is the C# version of your code:
public static void IncludeNewFiles()
{
int count = 0;
EnvDTE80.DTE2 dte2;
List<string> newfiles;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
foreach (Project project in dte2.Solution.Projects)
{
if (project.UniqueName.EndsWith(".csproj"))
{
newfiles = GetFilesNotInProject(project);
foreach (var file in newfiles)
project.ProjectItems.AddFromFile(file);
count += newfiles.Count;
}
}
dte2.StatusBar.Text = String.Format("{0} new file{1} included in the project.", count, (count == 1 ? "" : "s"));
}
public static List<string> GetAllProjectFiles(ProjectItems projectItems, string extension)
{
List<string> returnValue = new List<string>();
foreach(ProjectItem projectItem in projectItems)
{
for (short i = 1; i <= projectItems.Count; i++)
{
string fileName = projectItem.FileNames[i];
if (Path.GetExtension(fileName).ToLower() == extension)
returnValue.Add(fileName);
}
returnValue.AddRange(GetAllProjectFiles(projectItem.ProjectItems, extension));
}
return returnValue;
}
public static List<string> GetFilesNotInProject(Project project)
{
List<string> returnValue = new List<string>();
string startPath = Path.GetDirectoryName(project.FullName);
List<string> projectFiles = GetAllProjectFiles(project.ProjectItems, ".cs");
foreach (var file in Directory.GetFiles(startPath, "*.cs", SearchOption.AllDirectories))
if (!projectFiles.Contains(file)) returnValue.Add(file);
return returnValue;
}
Thanks to #JaredPar and #lpthnc for pointing me in the right direction. I ended up using an approach very similar to what #JaredPar outlines above. Here's my working macro FWIW.
Imports System.IO
Imports System.Collections.Generic
Imports EnvDTE
Public Module Main
Sub IncludeNewFiles()
Dim Count As Integer = 0
For Each Project As Project In DTE.Solution.Projects
If Project.UniqueName.EndsWith(".vbproj") Then
Dim NewFiles As List(Of String) = GetFilesNotInProject(Project)
For Each File In NewFiles
Project.ProjectItems.AddFromFile(File)
Next
Count += NewFiles.Count
End If
Next
DTE.StatusBar.Text = String.Format("{0} new file{1} included in the project.", Count, If(Count = 1, "", "s"))
End Sub
Private Function GetAllProjectFiles(ByVal ProjectItems As ProjectItems, ByVal Extension As String) As List(Of String)
GetAllProjectFiles = New List(Of String)
For Each ProjectItem As ProjectItem In ProjectItems
For i As Integer = 1 To ProjectItem.FileCount
Dim FileName As String = ProjectItem.FileNames(i)
If Path.GetExtension(fileName).ToLower = Extension Then
GetAllProjectFiles.Add(fileName)
End If
Next
GetAllProjectFiles.AddRange(GetAllProjectFiles(ProjectItem.ProjectItems, Extension))
Next
End Function
Private Function GetFilesNotInProject(ByVal Project As Project) As List(Of String)
Dim StartPath As String = Path.GetDirectoryName(Project.FullName)
Dim ProjectFiles As List(Of String) = GetAllProjectFiles(Project.ProjectItems, ".vb")
GetFilesNotInProject = New List(Of String)
For Each file In Directory.GetFiles(StartPath, "*.vb", SearchOption.AllDirectories)
If Not ProjectFiles.Contains(file) Then GetFilesNotInProject.Add(file)
Next
End Function
End Module
The approach I would take is to
Enumerate the file system looking for all files
Check and see if the given file has an associated project item.
Here is a quick bit of sample code
Function ContainsItem(p as Project, fileName as String) As Boolean
Try
Return p.ProjectItems.Item(fileName)
Catch ex As ArgumentException
Return False
End Try
End Function
Function CotainsItem(dte as DTE, fileName as String) As Boolean
For Each p As Project in dte.Solution.Projects
Return ContainsItem(p, fileName)
Next
End Function
Function GetFilesNotInProject(dte as DTE, startPath as String) as List(Of String)
Dim list As New List(Of String)
Dim files = Directory.GetFiles(startPath, "*.cs", SearchOPtions.AllDirectories)
For Each file in files
If Not ContainsItem(dte, file) Then
list.Add(file)
End If
Next
Return list
End Function
I'd go with PowerShell. The PowerShell script in my other post will do this for you. The script will get the list of included files from the project file and compare that against the files on disk. You will get the set of files that are on disk but not included in the project. You can either delete them or pend them as deletes for TFS.
https://stackoverflow.com/a/23420956/846428
Related
I am trying to implement a simple save-load function in my application, which would save the states of various GUI elements in my application (textboxes, checkboxes, dropdown menus, and so on) to a custom-named .txt file, and then load them back the next time user runs my application. I do not want to use My.Settings, because it is a portable application, and therefore the settings file has to be next to the executable. Also because my application is an editor, and the settings have to be bound by name to the current file the user is working with.
Write permissions is not an issue. I want to code this in a way so that I would only have to write down the names of the GUI elements to be mentioned once in my code, preferably in a list. Like this (pseudo-code):
'list
Dim ElementsToSave() as Object = {
Textbox1.text,
Checkbox1.Checked,
DropDownMenu1.SelectedItem,
.....
}
'save-load sub
Sub SaveLoad(Elements as Object, mode as string)
If mode = "save" then
For each Element in Elements
??? 'save each element state to .txt file
Next
If mode = "load" then
For each Element in Elements
??? 'load each element state from .txt file
Next
End if
End sub
'call
SaveLoad(ElementsToSave, "save")
'or
SaveLoad(ElementsToSave, "load")
I hope this conveys what I'm trying to achieve. Can anyone give any advice on how to make this work, please?
EDIT: I forgot to mention. It would be very nice if each value in the .txt file would be saved with a key that refers to a specific element, so that if I add more GUI elements in my application in the future, or re-arrange them, this save-load sub would always choose the correct value from the .txt file for a specific element.
using System.IO;
...
private enum ControlProperty
{
None = 0,
Text = 1,
Checked = 2,
SelectedValue = 3
}
private string GetSettingsFile()
{
FileInfo fi = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
string path = Path.Combine(fi.Directory.FullName, "settings.txt");
return path;
}
private void test()
{
SaveSettings();
LoadSettings();
}
private void SaveSettings()
{
object[] vals = new object[] { this.Textbox1, ControlProperty.Text, this.Textbox1.Text, this.Checkbox1, ControlProperty.Checked, this.Checkbox1.Checked, this.Menu1, ControlProperty.SelectedValue, this.Menu1.SelectedValue };
string txt = "";
for (int i = 0; i < vals.Length; i += 3)
{
string controlID = (vals[i] as Control).ID;
ControlProperty property = (ControlProperty)vals[i + 1];
object state = vals[i + 2];
txt += controlID + ":" + property.ToString() + ":" + state.ToString() + "\n";
}
string file = GetSettingsFile();
File.WriteAllText(file, txt);
}
private void LoadSettings()
{
string file = GetSettingsFile();
string[] lines = File.ReadAllLines(file);
foreach (string s in lines)
{
string[] parts = s.Split(':');
if (parts.Length < 3) continue;
string id = parts[0];
var c = this.form1.FindControl(id);
ControlProperty prop = ControlProperty.None;
Enum.TryParse<ControlProperty>(parts[1], out prop);
string state = parts[2];
if (c is TextBox && prop == ControlProperty.Text)
{
TextBox t = c as TextBox;
t.Text = state;
}
else if (c is CheckBox && prop == ControlProperty.Checked)
{
CheckBox chk = c as CheckBox;
chk.Checked = state == "True";
}
else if (c is Menu && prop == ControlProperty.SelectedValue)
{
Menu m = c as Menu;
foreach (MenuItem menuItem in m.Items)
{
if (menuItem.Value == state)
{
menuItem.Selected = true;
}
}
}
}
}
I am trying to use Parallel.For() to load n # of flat files into a database. Each iteration needs to create 2 config files in a folder and then run another process that uses the config files to know how to load the flat file into Oracle. Before I decided to use a parallel option, I tested the performance benefits by iterating 1000 times, each time creating the config files. I had better than a 2x increase in speed.
I then added the part where I call the other process, and I am getting periodic errors that some of the config files I am trying to create per iteration aren't there.
Here is my stripped-down code:
public static void FindFilesToLoad(int monitorId)
{
//left out code here that calls a stored procedure to get a list of ids
fileCount = idList.Count;
if (fileCount != 0)
{
Parallel.For(0, fileCount,
i =>
{
fileId = Convert.ToInt32(idList[i]);
//do the work here...
LoadFileIntoDatabase(monitorId, fileId);
});
}
}
public static void LoadFileIntoDatabase(int monitorId, int fileId)
{
stamp = Guid.NewGuid().ToString();
controlFile = CreateSqlLoaderControlFile(monitorId,stamp);
controlFileName = Path.GetFileName(controlFile);
parFile = CreateParFile(monitorId, controlFileName, stamp);
myCommand = #"CMD.EXE";
ProcessStartInfo startInfo = new ProcessStartInfo(myCommand)
{
WorkingDirectory = #"c:\temp\",
Arguments = #"/c SQLLDR CONTROL=" + controlFile + " PARFILE=" + parFile ,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
exitCode = process.ExitCode;
}
Error example:
SQLLoader-500: Unable to open file (6f46ccfd-986e-427e-ab63-b7ce2396488e.ctl)
SQLLoader-553: file not found
SQL*Loader-509: System error: The system cannot find the file specified
I am creating folder in microsoft outlook through .net application BUT it is getting displayed ONLY when I Restart outlook. New folder should be displayed without restarting outlook.
I am using below code:
Outlook.NameSpace nameSpace = OutlookApp.GetNamespace("MAPI");
Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Folders inboxFolders = folderInbox.Folders;
Outlook.MAPIFolder subfolderInbox = null;
Outlook.Application oApp;
oApp = nameSpace.Application;
Outlook.Explorer exp = oApp.ActiveExplorer();
subfolderInbox = inboxFolders.Add("InboxSubfolder",
Outlook.OlDefaultFolders.olFolderInbox);
exp.CurrentFolder = subfolderInbox;
I see you are using Add-in Express.
I attached your code to an adxRibbonButton control and it worked for me.
How are you calling this code?
I see some unnecessary object creation and removed them in my edited version below.
Also - you want to make sure to release the Office objects you create.
I recommend updating the code as follows:
private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)
{
Outlook.NameSpace nameSpace = OutlookApp.Session; //OutlookApp.GetNamespace("MAPI");
Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Folders inboxFolders = folderInbox.Folders;
Outlook.MAPIFolder subfolderInbox = null;
//Outlook.Application oApp;
//oApp = nameSpace.Application;
Outlook.Explorer exp = OutlookApp.ActiveExplorer(); //oApp.ActiveExplorer();
subfolderInbox = inboxFolders.Add("InboxSubfolder",
Outlook.OlDefaultFolders.olFolderInbox);
exp.CurrentFolder = subfolderInbox;
//Release COM Objects
if (exp != null) Marshal.ReleaseComObject(exp);
if (subfolderInbox != null) Marshal.ReleaseComObject(subfolderInbox);
if (inboxFolders != null) Marshal.ReleaseComObject(inboxFolders);
if (folderInbox != null) Marshal.ReleaseComObject(folderInbox);
if (nameSpace != null) Marshal.ReleaseComObject(nameSpace);
}
We currently use the code below to download files from a network server to the client in an old ASP.net page.
We have re-written the app in MVC3 and would like to upgrade this functionality. I have seen several posts that claim you can access the file from the network share by writing the following lines in web.config
<authentication mode="Windows"/>
<identity impersonate="true" userName="" password="" />
However, we are currently using Forms authentication mode to logon to the site. Would that interfere with the login functionality?
Here's our download code.
Partial Class DownloadFile2
Inherits System.Web.UI.Page
Private BufferSize As Integer = 32 * 1024
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim path As String = Request.Params("File")
path = "\\speedy\wanfiles\" + path.Substring(3)
Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)
Dim Buffer(BufferSize) As Byte
Dim SizeWritten, fileindex As Integer
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name)
Response.AddHeader("Content-Length", file.Length.ToString)
Response.ContentType = "application/octet-type"
Response.Flush()
fileindex = 0
Do
// not sure about this GM.AlasData code below
SizeWritten = GM.AlasData.ReadFileBlock(file.FullName, Buffer, fileindex, BufferSize)
Response.OutputStream.Write(Buffer, 0, SizeWritten)
Response.Flush()
If SizeWritten < BufferSize Then
Exit Do
Else
fileindex = fileindex + SizeWritten
End If
Loop
Response.End()
End Sub
End Class
I found this code to do downloads using MVC3 but cannot access the file because it is considered a local file.
public FileResult Download(string FilePath)
{
if (FilePath != null)
{
string path = FilePath;
string contentType;
// files are stored on network server named speedy
path = string.Concat(#"\\speedy\files\", HttpUtility.UrlDecode(path));
System.IO.FileInfo file = new System.IO.FileInfo(path);
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(file.Extension.ToLower());
if (rk != null && rk.GetValue("Content Type") != null)
{
contentType = rk.GetValue("Content Type").ToString();
}
else
{
contentType = "application/octet-type";
}
//Parameters to file are
//1. The File Path on the File Server
//2. The content type MIME type
//3. The parameter for the file save by the browser
return File(file.FullName, contentType, file.Name);
}
else
{
return null;
}
}
What needs to be done to fix this where we can perform this functionality?
You can remove the Authentication mode = windows line since you are using forms authentication. Use the impersonation and make sure the account has read access to the network location you are trying to access. that should work.
http://www.codeproject.com/Articles/4051/Windows-Impersonation-using-C
I'm trying to write a macro that will generate a plain-text list of files changed based on the list of files in the Pending Changes pane but I can't figure out how to do it. The server location of a file is the property that is formatted like this:
$/TfsName/SomeSolution/Web/SomeFolder/SomeFile1.aspx
$/TfsName/SomeSolution/Web/SomeFolder/SomeFile2.aspx
The closest I can get is opening the properties of the selected item in the pane, which isn't very useful:
DTE.ExecuteCommand ("TeamFoundationContextMenus.SourceControlPendingChangesSourceFiles.TfsContextPendingCheckinsPendingCheckinsProperties")
Edit: here's the entire code for the macro I have so far, the TODOs are where I need help:
Public Class Pending
Public Shared Sub Pending()
OutputClear()
OutputWriteLine("Files Changed:")
Dim outInfo As String = ""
DTE.Windows.Item("{2456BD12-ECF7-4988-A4A6-67D49173F564}").Activate() 'Pending Changes - Source Files
'TODO: loop through each changed file
'TODO: get TFS server location of each file
outInfo &= "some file name"
OutputWriteLine(outInfo)
End Sub
' snip: other supporting functions
End Class
Well I haven't been able to figure out how to do it with a macro yet, but thanks to Bob Hardister on twitter, I can use this command to get what I'm looking for:
"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe" status $/ProjectName/SubDir/ /recursive
...but what works even better is a command-line app that uses this code:
const string TFSSERVER = "http://TfsServer:8080";
static void Main(string[] args)
{
//http://blogs.msdn.com/b/buckh/archive/2006/03/15/552288.aspx
//http://blogs.msdn.com/b/jmanning/archive/2005/12/01/499033.aspx
string projectName = args[0];
TeamFoundationServer tfs = new TeamFoundationServer(TFSSERVER);
VersionControlServer versionControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
PendingSet[] sets = versionControl.GetPendingSets(new String[] { "$/Projects/" + projectName }, RecursionType.Full);
Console.WriteLine(versionControl.AuthenticatedUser + " pending changes for " + projectName + ":");
foreach (PendingSet set in sets)
{
if (set.Type == PendingSetType.Workspace && set.OwnerName == versionControl.AuthenticatedUser)
{
foreach (PendingChange pc in set.PendingChanges)
{
Console.WriteLine(pc.ServerItem);
}
}
}
}
Then I just added the compiled EXE call to the External Tools menu and use it within VS there.
Bonus Edit: Here's the VSS version (not as nice):
const string SSDIR = #"\\VssServer\VssShare";
static void Main(string[] args)
{
string projectName = args[0];
string userName = "user";
VSSDatabaseClass vss = new VSSDatabaseClass();
vss.Open(SSDIR + #"\srcsafe.ini", userName, userName);
VSSItem sourceItem = vss.get_VSSItem("$/Projects/" + projectName, false);
Console.WriteLine(userName + " pending checkins for " + projectName + ":");
int total = GetItems(sourceItem);
Console.WriteLine(total.ToString() + " total changes.");
}
const int VSSFILE_CHECKEDOUT_ME = 2;
const int VSSITEM_PROJECT = 0;
const int VSSITEM_FILE = 1;
public static int GetItems(IVSSItem originalItem)
{
int total = 0;
foreach (IVSSItem subItem in originalItem.get_Items(false))
{
if (subItem.Type == VSSITEM_FILE && subItem.IsCheckedOut == VSSFILE_CHECKEDOUT_ME)
{
Console.WriteLine(subItem.Spec);
total++;
}
else if (subItem.Type == VSSITEM_PROJECT)
{
total += GetItems(subItem);
}
}
return total;
}