Is it safe to programmatically reference the public folder through:
Directory = System.Environment.GetEnvironmentVariable("public")+"MyCompanyName" // etc.
or is there a better way?
Again, what if someone deletes the environment variable for public, and is this safe to use for different language OSs?
This follows: How to install to the Public directory in Windows 7 from the VS 2010 deployment Setup Project
This seems a tad questionable, but it should work:
// This should give you something like C:\Users\Public\Documents
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
var directory = new DirectoryInfo(documentsPath);
// Now this should give you something like C:\Users\Public
string commonPath = directory.Parent.FullName;
It depends on what you want to achieve.
There is a enum called SpecialFolder. You can use it to get the Path to some Directories.
For Example:
System.Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
points to "C:\Users\Public\Desktop".
IMHO, your way isn't wrong, though i would do some Exception Handling in case the EnvVar is really missing.
Also you could use the ENUM with "CommonDesktopDirectory" and get rid of the "\Desktop" part.
Notice that the Environment.SpecialFolder.CommonDesktopDirectory is only available in .NET 4.0. For my .NET 3.5 systems (Windows 7 or XP) I used the registry key for the Shell Folders. My code snippet is in VB.NET.
Private mRegShellPath="Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
Private mCommonDesktop = Nothing
' dgp rev 3/8/2012
Private ReadOnly Property CommonDesktop As String
Get
If mCommonDesktop Is Nothing Then
Dim RegKey As RegistryKey
Try
RegKey = Registry.LocalMachine.OpenSubKey(mRegShellPath, False)
mCommonDesktop = RegKey.GetValue("Common Desktop")
Catch ex As Exception
mCommonDesktop = ""
End Try
End If
Return mCommonDesktop
End Get
End Property
If you want a place to put application-specific data that can accessed by all users, use as a base:
Environment.GetFolderPath(SpecialFolder.CommonApplicationData)
Also, consider using Path.Combine to combine elements to form a new path:
Path.Combine(
Environment.GetFolderPath(SpecialFolder.CommonApplicationData),
"MyCompanyName")
Have you looked at this ?
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
Specifies enumerated constants used to retrieve directory paths to system special folders.
Ie
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
You can get all these %wildcard% literals by looking into
Windows->Start-->regedit-->
Then, you perform
using System;
string path2Downloads = Environment.ExpandEnvironmentVariables(#"%USERPROFILE%\Downloads");
string path2Music = Environment.ExpandEnvironmentVariables(#"%USERPROFILE%\Music");
... and, so on .... and to test:
using System.IO;
string[] files = { "" };
if (Directory.Exists(path2Music)) {
files = Directory.GetFiles(path2Music);
}
Related
I want to get the path of an existing folder SeleniumTestData inside the solution.
Why? My selenium tests should create at start of the test, temporary folder which are being ignored in Git, so each of my colleagues has their own TestData folders for their own TestExecutions on their machine (Like Save/Load Cookies) and dont pull TestData from other colleagues.
The folder where i want to create other folder by code is named SeleniumTestData folder and is inside:
..\source\repos\CoffeeTalk\src\Tests
I cant hardcore the path, as i'm facing here 2 problems:
Tests are being ran in Windows and Docker (Linux)
Co-Workers are saving the solution in different windows directories
Now i need a general solution which will work in any of these cases.
I already tried: var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
which returned: D:\source\repos\CoffeeTalk\src\Tests\Web\CoffeeTalk.Client.Selenium.Tests\bin\Debug\net6.0
and then tried to navigate back by executing the codeline currentDirectory?.Parent about 5-6 times. But its then again different in Linux.
Now im looking for a clean way. I suppose the first way i did it was not wrong by getting the CurrentDirectory and navigate back.
I already searched for solutions using stackoverflow, google. Either the solutions are outdated or im not getting the result im expecting.
Here i have the method which creates the folder, but im struggling with the GetFolderPath method.
public static void CreateFolder(string folderName, string newFolderName)
{
var folderPath = GetFolderPath(folderName);
var pathCombined = Path.Combine(folderPath, newFolderName);
var folderExists = Directory.Exists(pathCombined);
if (folderExists) return;
Directory.CreateDirectory(pathCombined);
}
Directory.GetCurrentDirectory isn't the directory with your executable file. It's something else (I don't know) that by the way depends on the OS. You should use this instead:
using System.Reflection;
// ...
string exeDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
And then go up the folders hierarchy as you want:
string neededFolder = new DirectoryInfo(exeDirectory).Parent.Parent.Parent.ToString(); // Or more "Parent" calls
As far as I know, it works on different OSs.
I need to create a registry entry based on finding of 32/64-bit system from cake script. I can see the File operations reference, Directory operations reference in C# cake site. But i could not find the registry related reference in C# cake. Could anyone please let me know is there any option to make a registry entry using C# cake? If so, please specify the reference link. This will help me a lot to continue in cake script.
An alternative to using C# you could also be using the Reg.exe shipped with all major versions of Windows.
You could use this tool with Cake using StartProcess alias.
An example of doing this below:
DirectoryPath system32Path = Context.Environment
.GetSpecialPath(SpecialPath.Windows)
.Combine("System32");
FilePath regPath = system32Path.CombineWithFilePath("reg.exe");
string keyName = "HKEY_CURRENT_USER\\Software\\Cake";
string valueName = "Rocks";
string valueData = "1";
ProcessSettings regSettings = new ProcessSettings()
.WithArguments(
arguments => arguments
.Append("add")
.AppendQuoted(keyName)
.Append("/f")
.AppendSwitchQuoted("/v", valueName)
.AppendSwitchQuoted("/t", "REG_DWORD")
.AppendSwitchQuoted("/d", valueData)
);
int result = StartProcess(regPath, regSettings);
if (result == 0)
{
Information("Registry value successfully set");
}
else
{
Information("Failed to set registry value");
}
Currently, there are no Cake aliases for working with the registry. Having said that, there is nothing to stop you manipulating the Registry directly using that standard C# types.
An example of one such approach is here:
Writing to registry in a C# application
Cake provides a number of aliases for things that are more complicated to do, however, remember that almost everything that is provided in an alias could be done directly with C# in your main script. The aliases are simply there as a convenience.
Ok I know it is simple but I have forgotten how to do it. I want to create a folder in a directory but if I do "C:\Users\George\AppData\Roaming\myprogram" this only woks for me it will not work with everybody that I send it to
I tried "C:\Users\[User]\AppData\Roaming\Myprogram" but it says access denied so what can I use to make this work for everyone?
Here is the segment of code I am using to do this if it helps :
public Form2()
{
InitializeComponent();
Directory.CreateDirectory(#"C:\Users\[User]\AppData\Roaming\SkypeAdmin");
Directory.CreateDirectory(#"C:\Users\[User]\AppData\Roaming\SkypeAdmin\mem");
}
and I tried :
public Form2()
{
InitializeComponent();
Directory.CreateDirectory(#"C:\Users\User\AppData\Roaming\SkypeAdmin");
Directory.CreateDirectory(#"C:\Users\User\AppData\Roaming\SkypeAdmin\mem");
}
Your approach predefines a path for the folder so the location would only be valid for you and anyone else who has the specified loacation.
You can try this instead:
public Form1()
{
InitializeComponent();
DirectoryInfo di = Directory.CreateDirectory(skypeAdminPath);
DirectoryInfo di2 = Directory.CreateDirectory(skypeMemPath);
}
string skypeAdminPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "SkypeAdmin\\";
string skypeMemPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "SkypeAdmin\\mem\\";
Using Path.Combine:
string skypeAdminPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SkypeAdmin\\");
string skypeMemPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SkypeAdmin\\mem\\");
You can use CommonAppDataPath
string path = Application.CommonAppDataPath;//for folder with version
this may change every version of your application, if you dont want this behavior and you want Same Directory for all the version of your App you can use this
string path = Directory.GetParent(Application.CommonAppDataPath);
the directory will be readily available when your code executes above line, this is created on demand.
Note:
I assume that you want to create some directory for all users and use it. instead of doing so you can used shared Directory and access it from any user.
This is how a sample CommonAppData path look like
C:\ProgramData\MyCompany\WindowsFormsApplicationTest\1.0.0.0
Typically CommonAppDataBase\CompanyName\ProductName\Version
You need to use Enviroment.GetFolderPath for that:
Directory.CreateDirectory(Path.Combine(
Enviroment.GetFolderPath(Enviroment.SpecialFolder.ApplicationData),
#"\SkypeAdmin\"));
Directory.CreateDirectory(Path.Combine(
Enviroment.GetFolderPath(Enviroment.SpecialFolder.ApplicationData),
#"\SkypeAdmin\mem\"));
That does exactly what (I think) you mean to do. Your solution doesn't work because there is no folder called [User] or User in the system, so you can't create a folder in there, and Directory.CreateDirectory doesn't replace that for the user path for you.
I know that in the same directory where my code is being executed some files are located. I need to find them and pass to another method:
MyLib.dll
Target1.dll
Target2.dll
Foo(new[] { "..\\..\\Target1.dll", "..\\..\\Target2.dll" });
So I call System.IO.Directory.GetFiles(path, "*.dll"). But now I need to get know the path:
string path = new FileInfo((Assembly.GetExecutingAssembly().Location)).Directory.FullName)
but is there more short way?
You may try the Environment.CurrentDirectory property. Note that depending on the type of application (Console, WinForms, ASP.NET, Windows Service, ...) and the way it is run this might behave differently.
Environment.CurrentDirectory returns the current directory, not the directory where the executed code is located. If you use Directory.SetCurrentDirectory, or if you start the program using a shortcut where the directory is set this won't be the directory you are looking for.
Stick to your original solution. Hide the implementation (and make it shorter) using a property:
private DirectoryInfo ExecutingFolder
{
get
{
return new DirectoryInfo (
System.IO.Path.GetDirectoryName (
System.Reflection.Assembly.GetExecutingAssembly().Location));
}
}
Back in .NET 1.0 days I wrote a method to return the target of a shortcut on MS Windows. It did this through using an interop to the Windows Script Hosting Object Model and brute forced through the COM interface:
private FileInfo GetFileFromShortcut(FileInfo shortcut)
{
FileInfo targetFile = null;
try
{
IWshRuntimeLibrary.WshShell wShell = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.WshShortcut wShortcut = (IWshRuntimeLibrary.WshShortcut)wShell.CreateShortcut(shortcut.FullName);
// if the file wasn't a shortcut then the TargetPath comes back empty
string targetName = wShortcut.TargetPath;
if (targetName.Length > 0)
{
targetFile = new FileInfo(targetName);
}
}
catch (Exception)
{ // will return a null targetFile if anything goes wrong
}
return targetFile;
}
This still bugs me, and I was looking to replace this with something more elegant, but only if the replacement actually works at least as well. I still can't find a native C# way of finding the target of a shortcut. Is there one, or is this still the best way of doing this type of thing?
It looks like someone has written a class to manipulate shortcut files in C# called ShellLink, but it too uses COM.
Can't you just open the .lnk or .url file and parse it?
This talks about the same thing and shows what the files look like:
http://www.programmingtalk.com/showthread.php?t=7335
I got interested in this as well a while ago.
Here is the accepted response with a link to a (informal) description of the format of LNK files. Apparently, all available methods yet go through some API.