前言
最近因為寫到使用FileDialog開檔讀檔的關係,所以在打開時,會常常需要移動到資料夾所在路徑,因此就在想要如何才能指定開啟FileDialog
能夠就指定在想要的資料夾上,並且移動整個專案時,不會因為絕對路徑的關係發生錯誤,以下開始。
如何取得專案所在的資料夾路徑
方法有很多種,這邊介紹WinForm與Console模式下可是用的方式(有興趣可以去看參考資料的來源):string path = System.Windows.Forms.Application.StartupPath
string path = System.AppDomain.CurrentDomain.BaseDirectory
如何移動目前的路徑至上層
DirectoryInfo dir = new DirectoryInfo(System.Windows.Forms.Application.StartupPath);
string s = dir.Parent;
string s = dir.Parent.FullName;
OpenFileDialog dlg = new OpenFileDialog(); dlg.InitialDirectory = dir.Parent.Parent.FullName + @"\想要移動的其他目錄"; //指定FileDialog開啟時所在的目錄 dlg.RestoreDirectory = true;
沒有留言:
張貼留言