我有一個程式可以檢查是否創建了 .csv 檔案。如果創建了,做某事,如果不等到它創建。在 C# 中可能嗎?
嘗試過這樣的事情,但效果不佳......
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromMinutes(1);
var timer = new System.Threading.Timer((e) =>
{
if (!File.Exists(@"TestFileLocation"))
{
MethodName(); //Tried this to start method agian
}
else
{
HereIsRestOfTheCodeToDoSomeActions();
}
}, null, startTimeSpan, periodTimeSpan);
如果我的帖子不夠清楚,對不起,我的英語不流利,以下是步驟,程式將如何作業:
- 檢查檔案是否在檔案夾中創建
- 如果不是,請等待 1 分鐘,然后再次檢查
- 如果創建檔案做一些事情
- 從頭開始回圈這個步驟
uj5u.com熱心網友回復:
最適合您的問題是使用FileSystemWatcher。它的作業是監視目錄中的更改,例如在您的問題中需要它時創建檔案。
這也將使一分鐘閾值過時。考慮例如:
// necessary assembly using for using FileSystemWatcher classes.
using System.IO;
// ...
// instantiate your watcher.
using var watcher = new FileSystemWatcher(@"C:\path\to\folder");
// just filter for interesting attribtes.
watcher.NotifyFilter = NotifyFilters.FileName;
// attach an event handler to the event that is triggered when a file is created.
watcher.Created = OnCreated;
// ...
// implement the event handler.
private static void OnCreated(object sender, FileSystemEventArgs e)
{
// if the name is "your" name, you're ready to go ...
if (e.Name == "my_name")
// do something ...
}
但是,如果您仍想使用回圈(由于您未在帖子中說明的限制),那么它可能類似于以下不太可愛的方法:
var now = DateTime.Now;
var waitTime = TimeSpan.FromMinutes(1);
while (true)
{
if (!File.Exists(@"TestFileLocation"))
{
// do something ...
// exit the loop.
break;
}
Thread.Sleep(waitTime);
}
注意并注意這個無限回圈會阻塞主執行緒,直到你找到你的檔案。更好的方法是在主執行緒之外的單獨執行緒中執行該任務。
uj5u.com熱心網友回復:
創建 FileSystemWatcher https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=net-6.0并在 OnCreated 中添加檢查
uj5u.com熱心網友回復:
簡而言之,除了將代碼包裝到類中之外,此代碼與 @markwellman 相同。代碼不會等待,它會繼續監控,因此沒有理由停止和啟動。
在下面的示例中,在建構式中傳遞要監視的檔案夾和要監視的檔案檔案名FileOperations
using System;
using System.IO;
using static System.IO.Path;
namespace YourNamespace
{
public class FileOperations : FileSystemWatcher
{
/// <summary>
/// Path to check for file
/// </summary>
public string MonitorPath { get; set; }
/// <summary>
/// File name to watch for
/// </summary>
public string FileName { get; set; }
public FileOperations(string monitorPath, string fileName)
{
if (!Directory.Exists(monitorPath))
{
throw new Exception($"Missing {monitorPath}");
}
MonitorPath = monitorPath;
Created = OnCreated;
Path = MonitorPath;
Filter = fileName;
FileName = fileName;
EnableRaisingEvents = true;
NotifyFilter = NotifyFilters.FileName;
}
private void OnCreated(object sender, FileSystemEventArgs e)
{
var fileName = Combine(MonitorPath, GetFileName(e.FullPath));
if (fileName.Equals(Combine(MonitorPath,FileName), StringComparison.OrdinalIgnoreCase))
{
// TODO
}
}
public void Start()
{
EnableRaisingEvents = true;
}
public void Stop()
{
EnableRaisingEvents = false;
}
}
}
然后在一個表單中創建一個私有實體FileOperations。在表單關閉事件中呼叫停止方法。
public partial class Form1 : Form
{
private readonly FileOperations _fileOperations =
new FileOperations("Path to watch", "file name to trigger on");
public Form1()
{
InitializeComponent();
Closing = OnClosing;
}
private void OnClosing(object sender, CancelEventArgs e)
{
_fileOperations.Stop();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/483102.html
