Message in ASP.NET Text Box

I would like to have a text box in my C # web application if the file exists on the server. Any ideas? New in C # here. For example, if I have text.txt (and I know that it will always be text.txt), that someone gets into a folder on a file server, my web application page alerts me using a timer (or something in like that).

+3
source share
5 answers

The best way to check if one particular file exists is File.Exists:

if (File.Exists("c:\\test.txt"))
  //inform user
0
source

- -, , , - . , .

(.. -, -). JavaScript; , alert.

: JavaScript , . , JavaScript (setInterval), AJAX. "" - .aspx , HTML (, , "0" "1" ), , . JavaScript , , .

, AJAX, , jQuery. , , , , AJAX.

+2

For this purpose you can use the FileSystemWatcher class . But this should run as a client application (Windows Forms application or service), and not from a web application (you cannot access the client computer from your browser).

+1
source

try it

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 this helps

+1
source
// 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!");
}
+1
source

Source: https://habr.com/ru/post/1792014/


All Articles