日新阁

取日三省之意,记生活珠玑,每日清新,谓日新阁.

 

c#几个常用的代码

1、获取“我的文档”等一些系统文件夹路径
Environment.SpecialFolder中包含了一些系统文件夹信息
MessageBox.Show(Environment.GetFolderPath( Environment.SpecialFolder.Personal ));

2、获取 硬盘临时文件夹
this.p_runFilePath=System.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)+@"\Content.IE5\aaa";

3、获取应用程序当前执行的路径
string appPath = Application.ExecutablePath;

4、 如何确定当前运行的系统
OperatingSystem os = Environment.OSVersion;
MessageBox.Show(os.Version.ToString());
MessageBox.Show(os.Platform.ToString());

5、从完整的路径中获取文件名
用System.IO.Path.GetFileName 和 System.IO.Path.GetFileNameWithoutExtension(无扩展名)的方法

6、从完整的路径中获取文件扩展名
用System.IO.Path.GetExtension方法

7、被执行的EXE文件所在的目录。

如果是WinForm,可以使用System.Windows.Forms.Application类提供的StartupPath属性。

此外,AppDomain.CurrentDomain.BaseDirectory 属性也可以获得EXE文件所在的目录。

Directory.GetCurrentDirectory(); 获取应用程序的当前工作目录,
应该是应用程序最后一次操作过的目录。(比如在操作 OpenFileDialog 后就会改变。)
最后一位[ 不 ]包括"\"

AppDomain.CurrentDomain.BaseDirectory; 程序集的基目录,
最后一位包括"\"

string savingPath = AppDomain.CurrentDomain.BaseDirectory;
if(!savingPath.EndsWith(@"\"))
{
savingPath += @"\";
}
savingPath += "TL_"+DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");

if(!Directory.Exists(savingPath))
{
Directory.CreateDirectory(savingPath);
}

posted on 2007-03-30 14:15  flashicp  阅读(344)  评论(1编辑  收藏  举报

导航