Find if a file is present
-
Tuesday, March 06, 2012 1:05 AM
I was wondering how to find if a file is present in the C:\ drive. And if it is, display a picture box on the form. What do I need to include and what do I need to use, and where do I place the code? Thanks.
All Replies
-
Tuesday, March 06, 2012 1:20 AM
- Proposed As Answer by Konrad NeitzelMicrosoft Community Contributor Tuesday, March 06, 2012 11:46 AM
-
Tuesday, March 06, 2012 4:09 AM
So you can do,
if(file->Exists("File.exe")){
//code here
}
Will it work in all files and folders? I want to make it goto C:\MyProgram\Add-Ons
-
Tuesday, March 06, 2012 10:28 AM
Make it like this,
if(File.Exists(@"C:\MyProgram\Add-Ons\File.exe") { // code here }http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
Hope it helps!!!
Regards Vallarasu S. FSharpMe.blogspot.com
-
Tuesday, March 06, 2012 11:46 AM
Hi,
you got already a pointer to File.Exists, but maybe you want your appluication to be aware on file changes. In such a case you could use the FileSystemWatcher (http://msdn.microsoft.com/en-us/library/System.IO.FileSystemWatcher.aspx). This could be usefull if you want to be informed in case a file is created or deleted while your application runs and it will take away the need to write your own code to check a file system over and over in a loop.
With kind regards,
Konrad
-
Tuesday, March 06, 2012 2:06 PM
I tried Vallarasu's method. Here are the errors:
error C2018: unknown character '0x40'
warning C4129: 'g' : unrecognized character escape sequence
warning C4129: 'p' : unrecognized character escape sequence
warning C4129: 'h' : unrecognized character escape sequence
I am currently using C++. Do I use it like: "if(File::Exist(@"C:\MyProgramFile\Add-Ons\File.exe)"?
---------- PressOnThis
-
Tuesday, March 06, 2012 2:16 PM
I tried this, but nothing happened... I tested it with a text document called "hi.txt" Le codez:
this->FileSystemWatcher->Filter = L"C:\\MyProgramName\\Add-ons\\hi.txt";
this->PlugInChecker->Path = L"C:\\MyProgramName\\Add-ons\\";
private: System::Void PlugInChecker_Created(System::Object^ sender, System::IO::FileSystemEventArgs^ e) {
MessageBox::Show("Yay! IT worked :P");
}
I'm probobly coding wrong. Help?
---------- PressOnThis
-
Tuesday, March 06, 2012 2:17 PM
Try this:
using namespace System; using namespace System::IO; int main() { String* path = S"c:\\temp\\MyTest.txt"; String* path2 = String::Concat(path, S"temp"); try { StreamWriter* sw = File::CreateText(path); if (sw) __try_cast<IDisposable*>(sw)->Dispose(); // Only do the Copy operation if the first file exists // and the second file does not. if (File::Exists(path)) { if (File::Exists(path2)) { Console::WriteLine(S"The target already exists"); } else { // Try to copy the file. File::Copy(path, path2); Console::WriteLine(S"{0} was copied to {1}.", path, path2); } } else { Console::WriteLine(S"The source file does not exist."); } } catch (Exception*) { Console::WriteLine(S"Double copying is not allowed, as expected."); } }
Mitja
- Marked As Answer by Bob Wu-MTMicrosoft Contingent Staff, Moderator Tuesday, April 03, 2012 3:25 AM
-
Wednesday, March 07, 2012 3:29 AMModerator
I tried Vallarasu's method. Here are the errors:
error C2018: unknown character '0x40'
warning C4129: 'g' : unrecognized character escape sequence
warning C4129: 'p' : unrecognized character escape sequence
warning C4129: 'h' : unrecognized character escape sequence
I am currently using C++. Do I use it like: "if(File::Exist(@"C:\MyProgramFile\Add-Ons\File.exe)"?
---------- PressOnThis
Hi PressOnThis,
The code in c++ should be
bool bReturn = System::IO::File::Exists("C:\\MyProgramFile\\Add-Ons\\File.exe");In addition, I suggest you ask c++ issue in the Visual C++ Forums which you can get better support.
Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
- Edited by Bob Wu-MTMicrosoft Contingent Staff, Moderator Wednesday, March 07, 2012 3:29 AM edit code


