C# create dir in %AppData% - c#

I want create directory (if not exist) in path AppData/Roaming/test. But my code doesn't work, I dont know why. Can you help me?
string path;
path = #"%AppData%\Roaming\test\";
path = Environment.ExpandEnvironmentVariables(path);
Console.WriteLine(path);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
This code doen't create dir.

%AppData% is a SpecialFolder.
change your code from:
path = #"%AppData%\Roaming\test\";
to:
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var path = Path.Combine(appDataPath, #"test\");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);

You should really use Environment.SpecialFolders to reach special folders rather than explicitly hard-coding a path.
Something like this would do the trick:
string path = Path.Combine(Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData), "test");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}

Related

How get the file name in the directory in winCE? (C#)

My application is running on the winCE platform and needs to search the file when it initialized, but it seems winCE is very different from windows.
Step1. Get the current directory
So first, I am using a code to get a directory:
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()) + "\\";
The path is "\Program Files\SmartDeviceProject1\".
But when I use console.write it shows "\Program Files\SmartDeviceProject1\". So it missed "\"
Step2. Trying to get all the file name in the path
FileInfo[] fileinfo = dir.GetFiles();
Step3. Use a loop and IF to identify the file exist or not.
Here is some code.
string filename = "bridgetool";
// string path = Directory.GetCurrentDirectory(); // This is not working in winCE
// Get the current path
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()) + "\\";
Console.Write(path);
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] fileinfo = dir.GetFiles();
foreach (FileInfo file in fileinfo)
{
if (filename == file.ToString())
{
return true;
}
}
Console.Write("Cannot find");
return false;
I got the right current path but the problem is that the path I get and path I printed out is different. I am not sure if it is a problem.
Is there any way to search the file name in the specific path?

How to go one step above in a folder path by using AppDomain.CurrentDomain.BaseDirectory in c#

I am using AppDomain.CurrentDomain.BaseDirectory and I want to go one step backwards but couldn't figure out how? Below is the example,
CODE :
string path = AppDomain.CurrentDomain.BaseDirectory;
RESULT :
"C:\\Mainline Code\\IxExpress\\.NET Applications\\IXTextIndexBuilder\\IXTextIndexBuilder\\bin\\Debug\\"
EXPECTED RESULT:
"C:\\Mainline Code\\IxExpress\\.NET Applications\\IXTextIndexBuilder\\IXTextIndexBuilder\\bin"
You can use something like the following to get the parent of a given directory:
string dirName = AppDomain.CurrentDomain.BaseDirectory; // Starting Dir
FileInfo fileInfo = new FileInfo(dirName);
DirectoryInfo parentDir = fileInfo.Directory.Parent;
string parentDirName = parentDir.FullName; // Parent of Starting Dir
Use following snippet:
string path = (new FileInfo(AppDomain.CurrentDomain.BaseDirectory)).Directory.Parent.FullName;
Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName

c# OpenFileDialogue Initial directory using relative path

How can I set the initial directory to be where my test data exists?
var relPath = System.IO.Path.Combine( Application.StartupPath, "../../" )
dlg.Title = "Open a Credit Card List";
dlg.InitialDirectory = relPath ;
The default directory it opens up to is where the .exe exists: Project2\Project2\bin\Debug
I want it to open up by default in the Project2 folder where my test data exists. But it does not allow me to move up a parent directory. How do I get around this?
You can use Directory.GetParent(string path)
string relPath = Directory.GetParent(Application.StartupPath).Parent.FullName;
Or using DirectoryInfo
DirectoryInfo drinfo =new DirectoryInfo(path);
DirectoryInfo twoLevelsUp =drinfo.Parent.Parent;
dlg.InitialDirectory = twoLevelsUp.FullName;;
To convert your relative path to an absolute path you can use Path.GetFullPath()
var relPath = System.IO.Path.Combine(Application.StartupPath, #"\..\..");
relPath = Path.GetFullPath(relPath);
dlg.InitialDirectory = relPath;
Alternatively if you wish to change the working directory to your test data:
Directory.SetCurrentDirectory(relPath);
More information:
http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
http://msdn.microsoft.com/en-us/library/system.io.directory.setcurrentdirectory.aspx

How can get the current virtual path without file name?

Request.Path will get the current Path name with file name such as:
C:/......./Personal/Items.aspx
How can I get the only Path name such as:
C:/......./Personal
You can use Path.GetDirectoryName to get the directory part of a path.
var path = System.IO.Path.GetDirectoryName(#"C:\Personal\Items.aspx");
// path is #"C:\Personal"
This will return the virtual path:
Page.TemplateSourceDirectory
See the below answers for the physical path.
Use the GetParent() method on Directory.
DirectoryInfo parent = Directory.GetParent(requestPath);
you can get Directory Path from System.IO.FileInfo
var fInfo = new System.IO.FileInfo(path);
var result = fInfo.DirectoryName;

Read & Write Writelog

How do I write the error faced into the log.txt?
Firstly, I get error file name from web config as follows:
string Errorlog = System.Configuration.ConfigurationManager.AppSettings["Errorlog.txt"];
Next, I try to get the full path of the text file; but i do not know which one will pull it out.
This is a few. I want to get the full path by not making it static.
//string path = Global.getLogFilePath();
//string path = Path.GetFileName(directoryFullPath);
//string path = openFileDialog.FileName;
//string path = Path.Combine(System.Environment.CurrentDirectory);
//string path = Path.GetFullPath("Errorlog.txt");
//string path = Directory.GetCurrentDirectory();
//string path = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName());
//string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
//string path = Environment.CurrentDirectory.ToString();
//string path = System.Environment.GetEnvironmentVariable("TEMP");
//if (!path.EndsWith("\\")) path += "\\";
Put log.txt in a Logs sub-folder of your web site and then you can get the absolute path like this:
string logFile = HttpContext.Current.Server.MapPath("~/Logs/log.txt");
Also don't forget to restrict the public access to the Logs folder to avoid anyone reading your application log files.

Categories