Tracking the location of a file in C# windows? - c#

I need to track the location of some files that i know the initial full paths. Situtation as follows :
I have a file in path C:\Temp\first.txt
in some time this first.txt file changes location to for example C:\Temptwo folder
i need to learn this second location automatically when the location change happens is there way to do it in C# thanks.

It depends on where the files are moving from and to. If you are moving from C:\\MyProgram\TempA\Temp.txt to C:\\MyProgram\TempB\Temp.txt, then you can just use the directory tools. Directory.GetFiles will scan a directory for you and return an array of strings containing the names of those files. If you know the name of the file you are looking for, or you are using a unique file extension, then you can search through the files you find until you identify the file you are looking for. You can also call Directory.GetDirectories recursively to look through sub-directories if you have a lot of folders this file could be hiding in.
While this might technically work on the C drive, I would suggest carefully considering your use case before trying to re-invent Windows Search.

Related

File path for c# when moved to a memory stick

sorry first time user and currently learning c# at uni, i'm working on an assignment and i'm trying to get the file path to work on the memory stick as that's what i need to hand it in on, thanks many regards
I'm not completely sure what your question is by I think what you're talking about is absolute vs relative paths. If you use an absolute path like "C:\users\yourname\blalba\project\stuff", then it's obviously only going to only work on your computer. However, you mostly all of the time want to use relative paths. Relative paths have the root directory of the build output files for your project; where your .exe file is built for your project. This is usually in "projectdir\bin\debug" or "projectdir\bin\release". So if you put for example a file called 'test.txt' in that directory, you can simply put the relative path "test.txt" instead of "C:\users\yourname\blalba\project\bin\debug\test.txt". If you were to put 'test.txt' in the project directory, you can use the relative path "....\test.txt". "..\" means navigating one step back.
The path to the directory containing the files you are looking for is "F:\Mod005244, 1715840". The "1715840 JH" is just the name of the drive. You access different drives via the drive letter. In this case, the drive letter "F" was assigned to your flash drive.
I would suggest making the file path configurable or request input from the user of the program as the drive letter of a flash drive will not always be "F". There are even classes (such as FileBrowserDialog) that will open a graphical file browser dialog and prompt the user to navigate and select a file.

C# Open a process without know location path

I have to open a process.exe from a button_click event by known only the filename and the .exe extension.
Is It possible? In two words i need to make a game.exe library ,and if the filename.exe will be found the process filename.exe,
need to be started.
I want to avoid to ask the costumer the full path of the program location, and i dont want to use OpenFileDialog()for browse. Im three days stuck here. So
filename.exe=textbox1.text
SystemDiagnostic.Process(..).Start(filename.exe)
this will open only the file if the file and the debug program are in the same directory.
Do i need a recursive search, and if the file is found open the process?How do i search for the filename.exe in all the #"C:\?
Thank everyone Sorry for bad english.
If that file is deployed by you, like it is up to you to make an installer for it, you can add the containing directory of that file to the PATH environment variable, then you can simply launch it by
Process.Start(“filename.exe”)
Without having to specify the full path to that file.
If it is out of your control, but there is some trace about the location of that file, for example, if it is from a known vendor and the file has a default location or there is something like “InstallLocation” in registry for that file, you can query registry to get the full path then launch it.
Your last resort will be scanning the whole disk for that file. But this is too bad, unreliable and slow...and things can go wrong easily. I strongly warn you against this approach.
If you have to choose the last approach, you might find this post useful. Remember to check other attributes like version number, publisher, etc., in case you end up calling a wrong file happens to have that name.

Finding a directory or file without name?

We have a some files or directories with their absolute paths.
These files or directories will be renamed in a process. We can't get the new names from the process at all, but we have the root directory full name(full absolute path)
Now, I wanna find that items again
Is there a unique key or something for directories or files to find them without the exact name?
No, there is nothing directly available on regular NTFS from C# code to do so.
You can
compute some sort of hashes of files yourself + size check to find them again after rename (if they simply renamed)
use events from FileSystemWatcher to track file movements
add alternative streams to files if they will not be stripped by "the process" to use as custom markers.

Find a file path

I'm creating a program that controls if words in a textbox are present in a text file.
The question is: Is there a way to find the path of that file without depending on the computer you're on?
If you don't use an absolute value for the path - it'll always try to resolve it relative to where your application is.
So if you put the file in the same folder as your application, it'll find it.
Otherwise, if you want your user to locate the file for you, you could use an OpenFileDialogExample here
Another option is to use one of the 'known' paths (such as My Documents). You can do this using Environment.GetFolder
But all of these depend on what you're trying to do exactly.
You can use relative paths. For example, if your path is "file.txt" and save the file, it will be saved next to your exe. Same thing for opening.

Comparing files in folder and moving them if size smaller

im new here, have been trying to compere one file to another and if previous file in folder is bigger then move the file. I was trying to find solution for vba, batch, c# but had no luck yet. there are many files in folder sorted by date and we need to compare one file to next and so on. If i could get ANY help on that i would greatly appreciate that!
For c#:
Use the static methods on the Directory class to get a list of the files in a folder.
Use the FileInfo class to get information about the files (IE: file size)
Use File.Move() to relocate the files if they match your criteria.
Have a look over the IOException and UnauthorizedAccessException documentation to see all the bad things that might happen when your program runs.

Categories