c# OpenFileDialogue Initial directory using relative path - c#

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

Related

How to get root directory of windows using C#?

DirectoryInfo di = new DirectoryInfo(#"c:\windows\temp");
Here I want to replace c:\ with the current drive on which the user is operating windows.
Is it possible to do that?
In .net you ought to use Path.GetTempPath to get a temporary path:
https://learn.microsoft.com/en-us/dotnet/api/system.io.path.gettemppath?view=net-7.0&tabs=windows
Both Environment.GetEnvironmentVariable("SystemRoot") and Environment.GetEnvironmentVariable("windir") should give you the path in form of "driveLetter:\\Windows"
So you can do:
DirectoryInfo di = new DirectoryInfo(Environment.GetEnvironmentVariable("SystemRoot"));
For the difference between "SystemRoot" and "windir" see:
https://superuser.com/questions/638321/what-is-difference-between-windir-and-systemroot
DirectoryInfo di2 = new DirectoryInfo(Environment.GetEnvironmentVariable("SystemRoot"));
string path = Convert.ToString(di2);
DirectoryInfo di = new DirectoryInfo( path + #"\temp");
I figured it out. Here is the corrected code.

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?

c# wpf read only folder

I am creating a new folder in documents and after that I want to write a file to it. The problem is, is that the folder I create is 'read only'. So I can't add a file to it. I can't fix it.
What I have now:
string target_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "data");
Directory.CreateDirectory(target_path);
var di = new DirectoryInfo(target_path);
di.Attributes &= ~FileAttributes.ReadOnly;
I also made my program startup as administrator but that doesn't make any difference.
Edit
When trying to store a file inside the folder I get the following error:
System.UnauthorizedAccessException: 'Access to the path "xxxx"' is denied.'
try explicitly set permissions.
string target_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "data");
DirectorySecurity securityRules = new DirectorySecurity();
securityRules.AddAccessRule(new FileSystemAccessRule("Users",FileSystemRights.FullControl, AccessControlType.Allow));
DirectoryInfo di = Directory.CreateDirectory(target_path, securityRules);

C# create dir in %AppData%

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);
}

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;

Categories