I have a string that is in the registry that can either point to an .exe, .txt along with certain command line arguments like:
C:\\PAthtoTheExe\program1.exe -arg1 -arg2
How do I determine if the "program1.exe" is a valid executable, IE that it points to an existing file? (this could be removed if the user uninstalls the application for eg:)
I don't like the idea of parsing out the arguments, because right now, the path can either point to an .exe or .txt or a .url. I would have to put in a long if else to do that.
There's a
File.Exists( path );
method that will return true if the filename exists. It won't tell you if the file contains a valid executable, though.
You can separate the path from the other arguments using
string tokens[] = registry_value.Split( ' ' );
then passing the first token to File.Exists()
Related
File.Copy(#"my program\\subfolder\\what i want to copy.txt", "C:\\Targetlocation");
How can i copy a text file from one folder to another using relative path.
To execute the File.Copy the source and destination will be a valid file path. in your case the destination is a folder not File. in this case you may get some exception like
Could not find a part of the path 'F:\New folder'
While executing the application, the current directory will be the bin folder. you need to specify the relative path from there. Let my program/subfolder be the folders in your solution, so the code for this will be like this:
string sourcePath = "../../my program/subfolder/what i want to copy.txt";
string destinationPath = #"C:\Targetlocation\copyFile.txt"
File.Copy(sourcePath, destinationPath );
Where ../ will help you to move one step back from the current directory. One more thing you have to care is the third optional parameter in the File.Copy method. By passing true for this parameter will help you to overwrite the contents of the existing file.Also make sure that the folder C:\Targetlocation is existing, as this will not create the folder for you.
File.Copy(#"subfolder\\what i want to copy.txt", "C:\\Targetlocation\\TargetFilePath.txt");
The sourceFileName and destFileName parameters can specify relative or
absolute path information. Relative path information is interpreted as
relative to the current working directory. This method does not
support wildcard characters in the parameters.
File.Copy on MSDN
Make sure your target directory exists. You can use Directory.CreateDirectory
Directory.CreateDirectory("C:\\Targetlocation");
With Directory.CreateDirectory(), you don't have to check if the directory exists. From documentation:
Any and all directories specified in path are created, unless they
already exist or unless some part of path is invalid. The path
parameter specifies a directory path, not a file path. If the
directory already exists, this method does nothing.
// Remove path from the file name.
string fName = f.Substring(sourceDir.Length + 1);
try
{
// Will not overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
}
You can provide the relative path from your current working directory which can be checked via Environment.CurrentDirectoy.
For example if your current working directory is D:\App, your source file location is D:\App\Res\Source.txt and your target location is D:\App\Res\Test\target.txt then your code snippet will be -
File.Copy(Res\\Source.txt, Res\\Test\\target.txt);
How can I check if a file exists or not in the directory the executable is?
I know how I could code it like this.
string path = Application.StartupPath + "config.cfg"
if(!FileExists(path))
{
//create the file
}
But the problem I am facing is that, the file is created every single time, even when the file exists, overwriting the data of the cfg file with the default ones.
You are not creating the possible file path properly. Use Path.Combine like:
string path = Path.Combine(Application.StartupPath, "config.cfg");
You are getting a path without terminating \ from Application.StartupPath, later you are concatenating the file name to it, This will create an invalid path, and since that doesn't exist, you check fails.
Just to show, the actual reason for getting the error, you can fix your code like:
string path = Application.StartupPath +"\\"+ "config.cfg";
But, do not use the above code, instead use Path.Combine to join multiple path elements.
I have path with backslashes and space that I need to send as parameter to regedit.exe:
\\folder1\folder2\folder three\file.reg
Based on my knowledge using # in front of string should allow back slashes to be specified directly (without escaping). Here is my full code that I am trying to execute it on:
string path = #"\\folder1\folder2\folder three\file.reg"
Process regeditProcess = Process.Start("regedit.exe", file);
When I tried running the program, it gives me an error from regedit's output saying:
Cannot import \folder1\folder2\folder: Error opening the file. There may be a disk or file system error
Since error report backslashes correctly I am guessing the compiler or regedit does not read anything past the whitespace after "folder"
When passing arguments in the command line, you need to surround them with ". Try this:
string path = #"""\\folder1\folder2\folder three\file.reg""";
Process regeditProcess = Process.Start("regedit.exe", path);
Adding "" in a verbatim string will add a single double quote to the string so the resulting string will be "\\folder1\folder2\folder three\file.reg" which will then allow it to get passed correctly to regedit.exe.
I want to copy a file to a directory. I thought it would be a simple enough process.
This is the code im using:
string strSrcPath = "C:\Users\Documents\Development\source\11.0.25.10\",
strDstPath = "C:\Users\Documents\Development\testing\11.0.25.10\",
strFile = "BuildLog.txt"
File.Copy(Path.Combine(sourcePath, sourceFile), strDstPath);
The problem here is that when i'm doing the File.Copy it wants to copy one file to another, but I dont want to do that since the file does not exist in the destination path. Therefore I get thrown an error which states something along the lines of 'Cannot copy, strDstPath is a destination not a file"
Was there something I could use instead of File.Copy to copy a file that doesnt exist in the destinaion from the source to destination?
The problem is that the parameters are the source filename and the destination filename. You are passing a destination directory and the program is confused because you can't make the file into a directory.
Use instead:
File.Copy(Path.Combine(strSrcPath , strFile ), Path.Combine(strDstPath, strFile);
You seem to be passing some wrong parameter to the Path.Combine (the second one). It should be strFile instead of sourceFile which is quite unclear where is it coming from.
And you also need to provide a filename for the destination folder:
File.Copy(Path.Combine(sourcePath, strFile), Path.Combine(strDstPath, strFile));
You also need to escape the \ characters in your string because your code will probably not compile. This could be done by either using \\ or by using the # character at the beginning of your string.
string strSrcPath = #"C:\Users\Documents\Development\source\11.0.25.10\",
strDstPath = #"C:\Users\Documents\Development\testing\11.0.25.10\",
strFile = "BuildLog.txt"
File.Copy(Path.Combine(sourcePath, strFile), Path.Combine(strDstPath, strFile));
Also make sure that the destination folder you specified exists. If it doesn't exist you need to create it first (using the Directory.CreateDirectory method).
You have to specify a filename for your destination
so
File.Copy("XMLFile1.xml", #"c:\temp");
will fail where
File.Copy("XMLFile1.xml", #"c:\temp\XMLFile1.xml");
will not
I wrote a simple console tool that reads a file and then writes something out. I intend to just drag and drop files and then out pops the output in the same directory as the input file.
All of the testing works, and when I call it from command-line, everything comes out as expected. However, when I tried dragging and dropping it in explorer, no files were created.
I did a search through the system and found that they were all dumped at Documents and Settings under my user folder, and when I printed out the full path that's what it said.
Which is weird. Wouldn't Path.GetFullPath return the absolute path of the input file? Instead it looks like it just combined that user directory path to the input's filename.
EDIT: here's the code. I feel like I've made a logic error somewhere but can't seem to see it.
filename = System.IO.Path.GetFileName(args[i]);
abspath = Path.GetFullPath(filename);
dirpath = Path.GetDirectoryName(abspath);
....
Console.WriteLine(dirpath);
Path.GetFullPath should return the absolute path of the path string you pass in.
Path.GetFileName(string path) only returns the filename and extension of the file you pass in. For example, System.IO.Path.GetFileName("C:\SomeDirectory\Test.txt"); would just return "Test.txt". You'll want to use the Path.GetDirectoryName to get the path of your input file, like so:
string inputDirectory = System.IO.Path.GetDirectoryName(args[i]);
Alternately, you can use the FileInfo class to retrieve a bunch more information about your input file. For example:
// Assuming args[i] = "C:\SomeDirectory\Test.txt"
FileInfo inputFile = new FileInfo(args[i]);
string inputDirectory = inputFile.DirectoryName; // "C:\SomeDirectory"
string inputFileName = inputFile.Name; // "Test.txt"
string fullInputFile = inputFile.FullName; // "C:\SomeDirectory\Test.txt"