Watching a directory using FileSystemWatcher - c#

Hello I'm watching a directory using FileSystemWatcher.
When a file is created into that directory - my watcher grabs and transfers it to the network drive.
My problem is that when a Microsoft-office file is opened, a temporary file is created in the watched directory. I can't find a way to ignore these files and also could not find when I should move these file to the network drive.

Temporary files have file attribute that cite it.
You can check for this attribute if the FileInfo.Attributes
check this FileAttributes

If I'm not mistaking, those temporary files are hidden files. If that doesn't work, you might consider allowing files with special names (those temporary files start with a tilde ~) to be ignored by your program. Using Regular Expressions you could set more "ignore masks" like that.

Related

Tracking the location of a file in C# windows?

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.

FileSystemWatcher Filter - Detect Zipped Files?

I'm using a FileSystemWatcher to detect that a text file is created in directory A and subsequently created in directory B.
The issue I'm having is, the process which moves the file from directory A to directory B also zips the file up, changing the filename from say "999_XXX_001.txt" to "999_XXX_001.txt.zip"
Three problems with this;
1) I can no longer open and read the file to analyse the contents
2) The filename has changed
3) The FileSystemWatcher appears to only support a single extension
Solution
Using two watchers, one for ".zip" and one for ".txt", I'm removing the .zip and comparing filenames because moved files no longer exist to be compared byte-for-byte.. I guess the real question here was how can I use the watcher to detect ".txt.zip" as an extension!
Why? You would have to wait until the process has finished its zipping magic and afterwards you can open the zip file with your framework of choice
Why is it a problem itself that the filename has changed?
No, the file watcher will detect any change of all files within the given directory
But maybe it is better to describe what you actually try to achieve here. There is probably a better solution to what you actually need.

Get a file path C#

I have a .txt file that I need to read in my program. For the moment I have the directory hardcoded as such:
file = new StreamReader(#"C:\Users\<username>\Documents\File.txt");
However that will (obviously) not work on any other PC that does not have that access to altering the code, or (by some strange happenstance) the same directory as the original code.
How can I get the full file path to set it in my program using C#?
You could create the file in their Application Data directory (they could still find it if they wanted to, but at least it wouldn't be as obvious as the My Documents folder).
When you want to access it, use the Environment class. There are methods for locating special folders for the current user, without resorting to hard-coded paths:
var filePath = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), "File.txt");
Option 1:
Application.StartupPath can be used for the purpose.
It gets the path for the executable file that started the application, not including the executable name.
Keep File.txt with your executable.
Option 2:
Use Environment.SpecialFolder.ApplicationData
It gives directory that serves as a common repository for application-specific data for the current roaming user.
NOTE: If you want to restrict the user to look into the contents of File.txt then you might need to encrypt the contents.

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.

Using WritePrivateProfileString to write path issue

I am using WritePrivateProfileString in c# (through DllImport) to store paths taken from textboxes on the interface. And the .ini file name is hardcoded in my application
string ini_file = ".\\config.ini";
However, when the file writing happens, the configuration file is written to the first path taken from the interface instead of writing it to the exe directory. Which is quite odd.
Debugging shows that the values are sent correctly to the WritePrivateProfileString but it still is written to the wrong location. Anyone knows why is that happenening?
I'd guess that something is changing the working directory of your process, most likely your code in the process. Note that the documentation has this to say:
If the lpFileName parameter does not contain a full path and file name for the file, WritePrivateProfileString searches the Windows directory for the file. If the file does not exist, this function creates the file in the Windows directory.
Now my guess is that this applies if you supply just a file name. Because your file name starts with . I believe that will force the function to start from the current working directory.
Having said all of that, and no matter what the cause of the problem is, you should use a fully-qualified path in order to make sure the file is written where you want it to be written. Whenever you want the file to go in a specific directory, it's always easiest to force that by using fully-qualified paths.
You can find the path to your executable using Application.ExecutablePath and then remove the file name part.
Another point to make is that the same directory as the executable may be a bad choice. If your program is installed under the Program Files directory then the directory which contains the executable will not be generally writeable. I think you should consider using a directory under in the user profile. Look for one of the Environment.SpecialFolder values.
Further to David Heffernan's answer - you can use
Path.GetDirectoryName(Application.ExecutablePath);
to safely get just the running application's folder part.
If you're in a dll rather than an executable, you can use
Path.GetDirectoryName(Assembly.GetAssembly(typeof(MyClass)).CodeBase);
Both require System.IO, and were originally posted here. Second example also requires System.Reflection).
Application data files are supposed to be written to the LocalApplicationData special folder.
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
You typically will not have permissions to write into the Program Files folder etc.

Categories