.csproj File - Programmatic adding/deleting files - c#

I've written a C# code generator to generate entities and related class files in our Ling-To-SQL application. The code generator needs to add/delete files in TFS and also add/delete them from a .csproj file so they are included or excluded properly in their respective projects.
I've figured out the TFS interaction but was curious as to the best or standard way of programmatically adding/removing files from .csproj files. Anyone have any experience with this?
Thanks - Randy

.csproj files are just XML, and they conform to an XSD. Adding the new XML elements should be all it takes.
To find the XSD take a look at: Where can I find the schema (XSD) for a .csproj file?

Another option is to use the Visual Studio automation model. From inside Visual Studio you can modify the project using macro code (VBA). This page on MSDN has links to the main automation libraries documentation.
Doing this, you could create a very slick integration. You could for instance have a Visual Studio macro that kicks off you code generation process and then adds the resulting files to the project.

It's all XML. Load it into a DOM and have your way with it.

I have seen code generators which do not modify the project. The project is always aware of the generated files, but the actual files are not included.

You create a sample file & then referring to it you can edit the the XML file
case ".aspx":
{
if ((File.ReadAllText((GlobalVariables.sDestinationPath).Trim() + "" + GlobalVariables.sProjName + "\\" + GlobalVariables.sProjName + projExtention).Contains("<Content Include=\"" + fileName1 + "\" />")) == false)
{
fileReader = File.ReadAllText(GlobalVariables.sDestinationPath + "" + GlobalVariables.sProjName + "\\" + GlobalVariables.sProjName + projExtention + "").Replace("<Content Include=\"Web.config\" />", "<Content Include=\"Web.config\" />" + "\n" + " <Content Include=\"" + fileName1 + "\" />");
File.WriteAllText(GlobalVariables.sDestinationPath.Trim() + "" + GlobalVariables.sProjName + "\\" + GlobalVariables.sProjName + projExtention + "", fileReader);
fileReader = File.ReadAllText(GlobalVariables.sDestinationPath.Trim() + "" + GlobalVariables.sProjName + "\\" + GlobalVariables.sProjName + projExtention + "").Replace("<Compile Include=\"" + pathfol + "\" />", "<Compile Include=\"" + pathfol + "\" />" + "\n" + " <Compile Include=\"" + fileName1 + ".vb\" > " + "\n" + " <DependentUpon>" + fileName1 + "</DependentUpon>" + "\n" + " <SubType>ASPXCodeBehind</SubType>" + "\n" + " </Compile>" + "\n" + " <Compile Include=\"" + fileName1 + ".designer.vb\">" + "\n" + " <DependentUpon>" + fileName1 + "</DependentUpon>" + "\n" + " </Compile> ");
File.WriteAllText(GlobalVariables.sDestinationPath.Trim() + "" + GlobalVariables.sProjName + "\\" + GlobalVariables.sProjName + projExtention + "", fileReader);
}

Related

DocuSign RestApi - Error while uploading document - TAB_REFERS_TO_MISSING_DOCUMENT

I'm getting the following error when I try to apply a template (created in my web account) to the document I'm trying to upload.
"DocumentId specified in the tab element does not refer to a document in this envelope. Tab refers to DocumentId 41791752 which is not present."
Any help would be greatly appreciated. Thanks.
Here's the code I have:
string xmlBody = "<envelopeDefinition xmlns=\"http://www.docusign.com/restapi\">" +
"<emailSubject>DocuSign API - Signature Request"</emailSubject>" +
"<status>sent</status>" + // "sent" to send immediately, "created" to save as draft in your account
"<compositeTemplates>" +
"<compositeTemplate>" +
"<serverTemplates>" +
"<serverTemplate>" +
"<sequence>1</sequence>" +
"<templateId>" + templateID + "</templateId>" +
"</serverTemplate>" +
"</serverTemplates>" +
"<inlineTemplates>" +
"<inlineTemplate>" +
"<sequence>2</sequence>" +
"<recipients>" +
"<signers>" +
"<signer>" +
"<recipientId>1</recipientId>" +
"<email>" + recipientEmail + "</email>" +
"<name>" + recipientName + "</name>" +
"<roleName>Signer</roleName>" +
"</signer>" +
"</signers>" +
"</recipients>" +
"</inlineTemplate>" +
"</inlineTemplates>" +
"<document>" +
"<name>" + documentName + "</name>" +
"<documentId>1</documentId>" +
"</document>" +
"</compositeTemplate>" +
"</compositeTemplates>" +
"</envelopeDefinition>";
The error message you quoted:
DocumentId specified in the tab element does not refer to a document in this envelope. Tab refers to DocumentId 41791752 which is not present.
I suggest that the template is referring to that documentId. But you're registering your document as Id 1.
Try changing
"<documentId>1</documentId>" +
to
"<documentId>41791752</documentId>" +

Regular expression getting html between two comments

I am trying to get a snippet of HTML between to comments.
I will need to parse the HTML between the start/end later.
I am actually reading from an html file but for test purposes I mocked the following up:
string emailFeedTxtStart = "<!--FEED FOR RECEIPT GOES HERE-->";
string emailFeedTxtEnd = "<!--FEED FOR RECEIPT ENDS HERE-->";
string html =
emailFeedTxtStart + Environment.NewLine +
#"<td align=""center"">" + Environment.NewLine +
#"<table style=""table-layout:fixed;width:380px"" border=""0"" cellspacing=""0"" cellpadding=""0"">" + Environment.NewLine +
"<tbody>" + Environment.NewLine +
"<tr>" + Environment.NewLine +
"<td>" + Environment.NewLine +
"</td>" + Environment.NewLine +
"</tr>" + Environment.NewLine +
"</tbody>" + Environment.NewLine +
"</table>" + Environment.NewLine +
"</td>" + Environment.NewLine +
emailFeedTxtEnd;
string patternstart = Regex.Escape(emailFeedTxtStart);
string patternend = Regex.Escape(emailFeedTxtEnd);
string regexexpr = patternstart + #"(.*?)" + patternend;
//string regexexpr = #"(?<=" + patternstart + ")(.*?)(?=" + patternend + ")";
MatchCollection matches = Regex.Matches(#html, #regexexpr);
matches returned is 0.
(note there is a lot more HTML between the ).
Any help would be greatly appreciated.
What are you going to parse the HTML with after? Because there's probably a way you can just do away with actually manipulating the HTML string beforehand. Here's a solution anyway:
string afterFirst = html.Substring(Regex.Match(html, emailFeedTxtStart).Index + emailFeedTxtStart.Length);
string between = afterFirst.Substring(0, Regex.Match(afterFirst, emailFeedTxtEnd).Index);

Save HTML EDITOR CONTENT to Content-type .DOCX in C#

private void ConvertHTMLtoDOCX(string txtcode)
{
System.Text.StringBuilder strBody = new System.Text.StringBuilder("");
strBody.Append("<html " + "xmlns:o='urn:schemas-microsoft-com:office:office' " + "xmlns:w='urn:schemas-microsoft-com:office:word'" + "xmlns='http://www.w3.org/TR/REC-html40'>" + "<head><title>Time</title>");
//The setting specifies document's view after it is downloaded as Print
//instead of the default Web Layout
strBody.Append("<!--[if gte mso 9]>" + "<xml>" + "<w:WordDocument>" + "<w:View>Print</w:View>" + "<w:DoNotOptimizeForBrowser/>" + "</w:WordDocument>" + "</xml>" + "<![endif]-->");
strBody.Append("<style>" + "<!-- /* Style Definitions */" + "#page Section1" + " {size:8.5in 11.0in; " + " margin:1.0in 1.25in 1.0in 1.25in ; " + " mso-header-margin:.5in; " + " mso-footer-margin:.5in; mso-paper-source:0;}" + " div.Section1" + " {page:Section1;}" + "-->" + "</style></head>");
strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" + "<div class=Section1>" + Html_editor.Content + "</div></body></html>");
//Force this content to be downloaded
//as a Word document with the name of your choice
string FullFilePath = #"C:\Users\ravikant\Desktop\AR GitHub\07-05-2014\FinalTestARGithub\LetterTemplate\"+ txtcode+ ".docx";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
ClientScript.RegisterStartupScript(this.GetType(), "disExp", "<script>alert('File Already Exists');</script>");
}
else
{
Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
Response.AppendHeader("Content-disposition", "inline; filename="+txtcode+".docx");
Response.Write(strBody);
}
}
Here is the code using the CONTENT-TYPE for .DOCX "application/vnd.openxmlformats-officedocument.wordprocessingml.document", the Content is corrupt while opening the file.
Try this to find out what is going on with the file.
I've done this with native word .docx, but not .docx generated in this manner, so it may or may not work.
Make a copy of the saved file, change its extention from .docx to .zip.
Try and open that. We are trying to find a file document.xml, which is normally in the "word" folder.
Open that in a text editor an see if anything is jumping out as wrong or try putting it through an XML validator. VisualStudio should be good enough to show any malformating.
Online XML Validator that might help: http://www.xmlvalidation.com/
The following lines are also suspect:
strBody.Append("<!--[if gte mso 9]>" + "<xml>" + "<w:WordDocument>" + "<w:View>Print</w:View>" + "<w:DoNotOptimizeForBrowser/>" + "</w:WordDocument>" + "</xml>" + "<![endif]-->");
As I'm unsure how word will handle IE conditional comments. Comment out or remove this line and see what happens.
strBody.Append("<style>" + "<!-- /* Style Definitions */" + "#page Section1" + " {size:8.5in 11.0in; " + " margin:1.0in 1.25in 1.0in 1.25in ; " + " mso-header-margin:.5in; " + " mso-footer-margin:.5in; mso-paper-source:0;}" + " div.Section1" + " {page:Section1;}" + "-->" + "</style></head>");
Due to the nested comments. <!-- /* */-->. Perhaps try changing it to: strBody.Append("</head>"); and see if that works.

Update a OneNote page, using the developer API

I tried to update a page in OneNote with the Microsoft reference :
http://msdn.microsoft.com/en-us/library/office/jj680118.aspx
Here's my problem. When i tried to update my page with the correct ID, it throwed me an error saying : Exception from HRESULT: 0x80042000.
Here is my code :
static void UpdatePageContent()
{
ApplicationClass onApplication = new ApplicationClass();
String strImportXML;
strImportXML = #"<?xml version="+"1.0"+" encoding="+"utf-16"+"?>" +
" <one:Page xmlns:one="+"http://schemas.microsoft.com/office/onenote/12/2004/onenote\""+"" +
"ID=\"{5BE09697-903A-45DD-88D4-8AD301A3D91F}{1}{B0}\">" +
" <one:PageSettings RTL=\"false\" color=\"automatic\">" +
" <one:PageSize>" +
" <one:Automatic/>" +
" </one:PageSize>" +
" <one:RuleLines visible=\"false\"/>" +
" </one:PageSettings>" +
" <one:Title style=\"font-family:Calibri;" +
" font-size:17.0pt\" lang=\"en-US\">" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[My Sample Page]]>" +
" </one:T>" +
" </one:OE>" +
" </one:Title>" +
" <one:Outline >" +
" <one:Position x=\"120\" y=\"160\"/>" +
" <one:Size width=\"120\" height=\"15\"/>" +
" <one:OEChildren>" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[Sample Text]]>" +
" </one:T>" +
" </one:OE>" +
" </one:OEChildren>" +
" </one:Outline>" +
" </one:Page>";
// Update page content
try
{
onApplication.UpdatePageContent(strImportXML, System.DateTime.MinValue);
}
catch (COMException e)
{
Console.WriteLine("Error Message : " + e.Message);
}
}
I really don't know how to solve this.
Your XML is not OneNote friendly.
Here's a list of error codes:
http://msdn.microsoft.com/en-us/library/office/jj680117.aspx
You can get rid of the first line, as #Sebastian has stated it's malformed anyway and my experience is that OneNote doesn't need it.
Also, remember that you don't need to send the entire page. You just need to send the page's objId and any updated objects. So one outline needs adding then this should also work:
"<one:Page xmlns:one=\"http://schemas.microsoft.com/office/onenote/12/2004/onenote\" +
"ID=\"{5BE09697-903A-45DD-88D4-8AD301A3D91F}{1}{B0}\">" +
" <one:Outline >" +
" <one:Position x=\"120\" y=\"160\"/>" +
" <one:Size width=\"120\" height=\"15\"/>" +
" <one:OEChildren>" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[New Text]]>" +
" </one:T>" +
" </one:OE>" +
" </one:OEChildren>" +
" </one:Outline>";
Just this new outline will get added.
If you still get problems (it doesn't complain but the content doesn't update) then check the extra parameters for UpdatePageContent, certainly in the 2013 API one can send a last modified date to check and also there's a parameter to force a local over-write.
There are some issues in the strImportXML string, which causes the update to fail.
Adjust
#"<?xml version="+"1.0"+" encoding="+"utf-16"+"?>" to "<?xml version=\"" + "1.0" + "\" encoding=\"" + "utf-16" + "\"?>"
Add an empty space before the page ID attribute ( + " " + "ID...
instead of + "" + "ID)
Make sure that the page ID is found/ present in your onApplication
hierarchy
Make sure that you reference the COM library with the matching
namespace defined in the one:Page element (e.g. Office 2013/ 15.0
Object Library has another namespace)

C#:Saving image to folder

HI. I know this is simple question but when I use
FirstPersonTestImage.Save(IIdComboBox.Text + "-" + i + ".jpg");
it works and saves file to folder where is the .exe file . But I want to save it to specific folder like /photo/IO-66/ and tryed to use
String StudentPath = PhotoPath + IGroupNoComboBox.Text + "/" + IIdComboBox.Text + "/" + IIdComboBox.Text + "-" + i + ".jpg";
FirstPersonTestImage.Save(StudentPath);
BUt it gives
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
How can I solve this problem? Is is about folder path ? or using "/" ?
EDIT
Here My code for creating and checking existing or not folder
if (!System.IO.Directory.Exists(PhotoPath + "/" + IGroupNoComboBox.Text.ToString().Trim()))
{
Directory.CreateDirectory(PhotoPath + "/" + IGroupNoComboBox.Text.ToString().Trim());
}
if (!System.IO.Directory.Exists(PhotoPath + "/" + IGroupNoComboBox.Text.ToString().Trim()+ "/" + IIdComboBox.Text.ToString().Trim() + "/"))
{
Directory.CreateDirectory(PhotoPath + "/" + IGroupNoComboBox.Text.ToString().Trim()+"/" + IIdComboBox.Text.ToString().Trim() + "/");
}
Instead of adding the path together manually, just use the IO.Path.Combine method and you don't have to worry about it.
If you're in VS2010 you can just call it with multiple parameters and otherwise you'll have to have nested calls.

Categories