不重载OnFileOpen 得到文件路径
你没有理解项目和程序的区别。
实际运行的程序是没有所谓项目的概念的(不可能把源代码也复制过去吧?!),也就是说,你的应用程序运行时的目录下一开始是不会自动地产生ClientPhoto这个文件夹的(除非你在项目里将这个文件夹中的文件设置为“复制到输出目录”。)
所以你要做的,第一件事就是先获取应用程序的路径,然后判断这个路径下的目录存在不存在,如果不存在,要首先创建一个。
我们知道“ClientPhoto”其实只是一个相对路径而已,要获得绝对路径,首先要知道应用程序当前的绝对路径:只需要检索System.Windows.Forms.Application.StartupPath这个属性即可。
string path = System.Windows.Forms.Application.StartupPath; 然后将应用程序当前路径path和字符串“ClientPhoto”组合成为一个完整的文件夹绝对路径folder(这时候就是绝对路径了,但是,还没完!)。
string folder = System.IO.Path.Combine(path, "ClientPhoto"); 接着判断 folder 指定的文件夹是否存在。
bool folderExists = System.IO.Directory.Exists(folder); 如果文件夹不存在,则创建它。如果创建不了,会引发异常,请自行捕捉这个异常。
if(!folderExists){ System.IO.Directory.CreateDirectory(folder);} 最后才返回路径 folder。
完整代码:
private string GetFolderPath(){ string folder = Path.Combine(Application.StartupPath, "ClientPhoto"); if(!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } return folder;}
button的click事件里的代码这样写就OK了
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开";
ofd.Filter="所有文件|*.*"; 这是设置扩展名。
if (ofd.ShowDialog() == DialogResult.OK)
{
TextBox1.text = ofd.FileName;
}
fd.GetPathName();
CDocument::GetPathName See Also
CDocument Overview | Class Members | Hierarchy Chart | CDocument::SetPathName
Call this function to get the fully qualified path of the document's disk file.
const CString& GetPathName( ) const;
Return Value
The document's fully qualified path. This string is empty if the document has not been saved or does not have a disk file associated with it.
-------------------------------------------
CDocument::GetTitle See Also
CDocument Overview | Class Members | Hierarchy Chart | CDocument::SetTitle
Call this function to get the document's title, which is usually derived from the document's filename.
const CString& GetTitle( ) const;
Return Value
The document's title.
--------------------------------
重载下面的函数
BOOL CEditviewDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
MessageBox(NULL, lpszPathName, "", NULL);//lpszPathName就是你要的文件名,记得要调用基类的OnOpenDocument 不然不会在view里面显示数据
return TRUE;
}