Directory.Exists导致编译错误

Anu*_*sha 1 c#

我正在做一个Asp.Net Web应用程序.我需要录制屏幕并保存录制的文件.我搜索了很多,从这里得到了一个最适合我的代码

这是代码

   public class ScreenRecorder
{

    private static string tempDir = Path.GetTempPath() + "/snapshot/";
    private static System.Threading.Thread snap = new System.Threading.Thread(Snapshot);

    private static System.Drawing.Rectangle _Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
    public static System.Drawing.Rectangle Bounds
    {
        get { return _Bounds; }
        set { _Bounds = value; }
    }

    private static void Snapshot()
    {
        if (!Directory.Exists(tempDir))
            Directory.CreateDirectory(tempDir);
        int Co = 0;
        do
        {
            Co += 1;
            System.Threading.Thread.Sleep(50);
            System.Drawing.Bitmap X = new System.Drawing.Bitmap(_Bounds.Width, _Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            using(System.Drawing.Graphics G = System.Drawing.Graphics.FromImage(X)) {
                G.CopyFromScreen(_Bounds.Location, new System.Drawing.Point(), _Bounds.Size);
                System.Drawing.Rectangle CurBounds = new System.Drawing.Rectangle(System.Drawing.Point.Subtract(System.Windows.Forms.Cursor.Position,Bounds.Size), System.Windows.Forms.Cursor.Current.Size);
                System.Windows.Forms.Cursors.Default.Draw(G, CurBounds);
           }
            System.IO.FileStream FS = new System.IO.FileStream(tempDir + FormatString(Co.ToString(), 5, '0') + ".png", System.IO.FileMode.OpenOrCreate);
            X.Save(FS, System.Drawing.Imaging.ImageFormat.Png);
            X.Dispose();
            FS.Close();
        } while (true);
    }

    public static void ClearRecording()
    {
        if (Directory.Exists(tempDir))
            Directory.Delete(tempDir, true);
            Directory.CreateDirectory(tempDir);
    }

    public static void Save(string Output)
    {
        System.Windows.Media.Imaging.GifBitmapEncoder G = new System.Windows.Media.Imaging.GifBitmapEncoder();

        List<System.IO.FileStream> X = new List<System.IO.FileStream>();
        foreach (string Fi in Directory.GetFiles(tempDir, "*.png", SearchOption.TopDirectoryOnly))
        {
            System.IO.FileStream TempStream = new System.IO.FileStream(Fi, System.IO.FileMode.Open);
            System.Windows.Media.Imaging.BitmapFrame Frame = System.Windows.Media.Imaging.BitmapFrame.Create(TempStream);
            X.Add(TempStream);
            G.Frames.Add(Frame);
        }
        System.IO.FileStream FS = new System.IO.FileStream(Output, System.IO.FileMode.OpenOrCreate);
        G.Save(FS);
        FS.Close();

        foreach (System.IO.FileStream St in X)
        {
            St.Close();

        }

    }

    public static void Start()
    {
        snap = new System.Threading.Thread(Snapshot);
        snap.Start();
    }

    public static void Stop()
    {
        snap.Abort();
    }

    private static string FormatString(string S, int places, char character)
    {
        if (S.Length >= places)
            return S;
        for (int X = S.Length; X <= places; X++)
        {
            S = character + S;
        }
        return S;
    }

}

class Program
{
    static void Main(string[] args)
    {
        ScreenRecorder.Start();
        System.Threading.Thread.Sleep(5000);
        ScreenRecorder.Stop();
        ScreenRecorder.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\video.gif");
        ScreenRecorder.ClearRecording();
    }
}
Run Code Online (Sandbox Code Playgroud)

我在asp.net中创建了一个新类并添加了上面的代码,但它引发了我的错误,如"当前上下文中不存在名称路径""当前上下文中不存在名称目录"

这是截图 码 如何解决此错误,我希望在单击按钮后记录屏幕.但是如何在按钮点击上调用类.请帮忙.!!提前致谢 !!

Ari*_*ria 5

因为要使用Directory的类,您必须参考 System.IO;

using System.IO;在后面的代码上添加此行,或者您可以单击该类并按下Ctrl+.它将在弹出菜单中建议您相关的命名空间.