I'd like to be able to have a text box alert in my C# web application if a file exists on a server. Any ideas? New to C# here. So for example, if I have text.txt (and I know that it's always going to be text.txt) that someone drops into a folder onto a fil开发者_JS百科e server, my web application page will alert me using the timer (or something like that).
Your web app is running on the Web server, which is probably in a locked server room somewhere. Showing a pop-up dialog there won't do anyone much good, because nobody will see it.
What you want is to show a dialog box on the client side (i.e., in the Web browser that's looking at your Web page). To do that, you have to use JavaScript; specifically, the alert function.
But now you've got a communication problem: your client-side JavaScript needs to be able to ask the server whether the file exists. Probably the best thing to do is to make a timer in JavaScript (setInterval) that sends an AJAX request to the server. You would have a "page" on the server -- some .aspx file -- that, instead of HTML, returns some simple code (maybe as simple as a "0" or a "1") that indicates whether the file exists. Then your JavaScript can load that "page's" content into a variable, inspect the variable, and know whether to show the alert.
As for how to do AJAX, you'll want to use a library like jQuery. Pick a library if you haven't already, and then read its docs to see how to do AJAX requests.
Try this
string path = "C:\\TestFolder\\......."; // Path
DirectoryInfo directory = new DirectoryInfo(path);
foreach (FileInfo file in directory.GetFiles())
{
if (file.Name == text.txt)
{
MessMessageBox.Show("Text file exists");
}
}
Hope it will help
// TODO: Read up on FileSystemWatcher
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\MyDirectory";
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
watcher.Renamed += new RenamedEventHandler(watcher_Renamed)
watcher.Created += new FileSystemEventHandler(watcher_Created);
watcher.EnableRaisingEvents = true;
watcher.Filter = "*.txt"; // could also set it to "text.txt" or "*"
void watcher_Changed(object sender, System.IO.FileSystemEventArgs e))
{
MessageBox.Show("Zomg " + e.FullPath +" has been changed!!");
}
private void fileWatcher_Renamed(object sender, System.IO.RenamedEventArgs e)
{
MessageBox.Show(e.OldFullPath + " was renamed to " + e.FullPath);
}
private void fileWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
MessageBox.Show(e.FullPath + " was deleted!");
}
private void fileWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
MessageBox.Show(e.FullPath + " was created!");
}
You can use the FileSystemWatcher class for this purpose. But that has to run as a client application (Windows Forms application or a service), not from a web application (you can't access the client computer from within your browser).
The best way to check if a single specific file exists would be File.Exists:
if (File.Exists("c:\\test.txt"))
//inform user
精彩评论