C# 获得%ProgramData% 路径

来源:互联网 发布:社交网络对社会的影响 编辑:程序博客网 时间:2024/05/20 21:49

法一:

string str = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);


法二:

private bool ProgramDataWriteFile(string contentToWrite)        {            try            {                string strProgramDataPath = "%PROGRAMDATA%";                string directoryPath = Environment.ExpandEnvironmentVariables(strProgramDataPath) + "\\MyApp\\";                string path = Environment.ExpandEnvironmentVariables(strProgramDataPath)+"\\MyApp\\ConnectionInfo.txt";                if (Directory.Exists(directoryPath))                {                    System.IO.StreamWriter file = new System.IO.StreamWriter(path);                    file.Write(contentToWrite);                    file.Close();                }                else                {                    Directory.CreateDirectory(directoryPath);                    System.IO.StreamWriter file = new System.IO.StreamWriter(path);                    file.Write(contentToWrite);                    file.Close();                }                return true;            }            catch (Exception e)            {            }            return false;        }

如果C:\ProgramData 上述都能获得  如果我的路径是 D:\ProgramData  能不能获取   ???





原创粉丝点击