编程实现自定义解决方案

作者:朱金灿

来源:blog.****.net/clever101

一般开发我喜欢遵循下面的规范:在项目解决方案文件夹下建5个文件夹:

文件夹名

备注

src

存放解决方案的源代码

thirdparty

存放第三方库的源代码(无源码的就存放头文件)

Doc

存放该项目的相关文档,我一般在Doc文件夹下又新建三个文件夹:

Design -- 项目设计方面的文档

Experience -- 项目经验总结

Other -- 项目其它方面的文档,包括参考资料方面的

Debug

Debug版输出文件夹

Release

Release版输出文件夹

但是使用VS 2005新建一个空白解决方案,往往是新建一个解决方案文件夹再加一个sln文件,我往往要手动建一个src文件夹,再将sln文件拷贝到该文件夹下,然后再新建thirdparty、Doc、Debug、Release,这样重复的的工作是程序员不愿意干的。我想到了使用编程实现这样的重复劳动。开始的想法大致就是指定一个sln文件存放文件夹,然后就自动生成一个项目解决方案文件夹,里面有src、thirdparty、Doc、Debug和Release等5文件夹,其中src文件夹下有一个sln文件,Doc文件夹下有Design、Experience和Other三个文件夹。

首先需要解决的难题是sln文件是如何生成的,开始我的想法是通过创建进程,调用devenv.exe的命令行用法来新建空白解决方案文件,但是上网搜索了一阵,感觉VS 2005的命令行用法太过于复杂,只好另想办法。我想到的第二种折中办法是先用VS 2005生成一个临时的sln文件Solution1.sln,放在我这个程序的exe文件所在目录下,然后到时通过拷贝sln文件的办法拷贝到用户的指定路径下,再重命名就行了。这样做的一个麻烦是到时发布程序时必须带两个文件:exe文件和Solution1.sln。想了一下,决定使用这样一种做法,把临时sln文件作为资源注入到exe文件,到时运行程序时再释放出来。

开始开发,新建一个基于对于框的工程SlnGen,然后使用VS 2005新建一个空白解决方案,生成一个Solution1.sln,再将Solution1.sln作为一种资源导入到SlnGen工程,具体操作如下:

1.在资源视图中选择"添加资源",然后在对话框中选中Solution1.sln,如下图:

编程实现自定义解决方案

<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL /> <w:BalanceSingleByteDoubleByteWidth /> <w:DoNotLeaveBackslashAlone /> <w:ULTrailSpace /> <w:DoNotExpandShiftReturn /> <w:AdjustLineHeightInTable /> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:UseFELayout /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!--[endif]-->

2.自定义一种资源:RT_SLN

编程实现自定义解决方案

<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL /> <w:BalanceSingleByteDoubleByteWidth /> <w:DoNotLeaveBackslashAlone /> <w:ULTrailSpace /> <w:DoNotExpandShiftReturn /> <w:AdjustLineHeightInTable /> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:UseFELayout /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]-->

<!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!--[endif]-->

3.按确定后生成一个新资源,其资源IDIDR_RT_SLN1

开始编码。其实程序的逻辑很简单:就是首先新建一个解决方案文件夹,然后新建一个src文件,再从资源中释放出sln文件,将其存放在src文件下,再新建余下的文件。

主体代码如下:

BOOL CSlnGenDlg::CreateFolder(CString &strFoldPath) { if(::PathFileExists(strFoldPath)==FALSE)//判断目录是否存在 { if(CreateDirectory(strFoldPath,NULL)==FALSE) { LPVOID lpMsgBuf; DWORD dw = GetLastError(); FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); MessageBox((LPCTSTR)lpMsgBuf,_T("系统错误"), MB_OK|MB_ICONSTOP); LocalFree(lpMsgBuf); return FALSE; } } return TRUE; } void CSlnGenDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 // 创建解决方案文件夹 if(!CreateFolder(m_PrjFolder)) return; // 创建src文件夹 CString strSrcFolder = m_PrjFolder + _T("//src"); // 从资源中释放解决方案文件 if (CreateFolder(strSrcFolder)) { HRSRC hRes=FindResource(theApp.m_hInstance,MAKEINTRESOURCE(IDR_RT_SLN1),_T("RT_SLN")); HGLOBAL hGlobal=LoadResource(theApp.m_hInstance,hRes); LPVOID lpData=LockResource(hGlobal); //lpData就指向你的文件起始处,现在把它写到一个临时文件中 HANDLE hFile=CreateFile(m_strSlnFilePath,GENERIC_WRITE,0,NULL,CREATE_NEW,0,0); DWORD dwWritten; DWORD dwfile_size = ::SizeofResource(theApp.m_hInstance,hRes); ::WriteFile(hFile,lpData,dwfile_size,&dwWritten,NULL); } // 创建Doc文件夹及其下面的Design、Experience和Other三个子文件夹 CString strDocFolder = m_PrjFolder + _T("//Doc"); if (CreateFolder(strDocFolder)) { CString strDesignFolder = strDocFolder + _T("//Design"); CreateFolder(strDesignFolder); CString strExperienceFolder = strDocFolder + _T("//Experience"); CreateFolder(strExperienceFolder); CString strOtherFolder = strDocFolder + _T("//Other"); CreateFolder(strOtherFolder); } // 创建Debug文件夹 CString strDebugFolder = m_PrjFolder + _T("//Debug"); CreateFolder(strDebugFolder); // 创建Release文件夹 CString strReleaseFolder = m_PrjFolder + _T("//Release"); CreateFolder(strReleaseFolder); // 创建thirdparty文件夹 CString strThirdpartyFolder = m_PrjFolder + _T("//thirdparty"); CreateFolder(strThirdpartyFolder); }

<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL /> <w:BalanceSingleByteDoubleByteWidth /> <w:DoNotLeaveBackslashAlone /> <w:ULTrailSpace /> <w:DoNotExpandShiftReturn /> <w:AdjustLineHeightInTable /> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:UseFELayout /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]-->

<!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!--[endif]-->

程序美中不足的是生成的sln文件在磁盘显示的是unrecognized version(未识别的文件版本)。不过这个丝毫不影响使用,我使用VS 2005打开后再新建一个工程,马上显示是VS 2005版本的解决方案文件。

程序界面如下:

编程实现自定义解决方案