You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于C#修改屏幕录制程序:取消保存对话框自动指定路径与文件名

Solution: Auto-Save Screen Recording Without Save Dialog

Hey there! I see you're adapting that screen recorder example to skip the save dialog and auto-save using the filename from your textbox and path from your config file. Let's get this sorted for you—since you're more comfortable with JS/HTML, I'll break this down clearly and simply.

Step-by-Step Fix

Here's how to modify your StartRecording method to ditch the dialog and auto-generate the save path:

  1. Remove the SaveFileDialog code entirely (you won't need it anymore)
  2. Safely build your full file path using Path.Combine (this avoids messy issues with missing slashes/backslashes across systems)
  3. Ensure the .avi extension is always present (even if the user forgets to type it)
  4. Add basic validation to prevent crashes from empty filenames or non-existent save paths
  5. Pass the constructed path directly to _writer.Open()

Modified C# Code

using System.Configuration;
using System.IO; // Don't forget to add this using statement for path-related methods

private string _name;
public string name 
{
    get { return _name; }
    set { Set(ref _name, value); }
}

// Initialize the config path once (move this to your window's constructor or a load event for efficiency)
string location = ConfigurationManager.AppSettings["LOCATION"];

private void StartRecording() 
{
    // First, validate inputs to avoid errors
    if (string.IsNullOrWhiteSpace(name))
    {
        // Show a user-friendly error (e.g., MessageBox.Show("Please enter a filename!"))
        return;
    }

    if (!Directory.Exists(location))
    {
        // Option 1: Create the directory automatically if it doesn't exist
        Directory.CreateDirectory(location);
        // Option 2: Show an error and exit (uncomment below if you prefer this)
        // MessageBox.Show("The save path from your config doesn't exist!");
        // return;
    }

    // Build the full save path: combine location + filename, and enforce .avi extension
    string fileNameWithExtension = Path.ChangeExtension(name, ".avi");
    string fullSavePath = Path.Combine(location, fileNameWithExtension);

    // Now start recording directly to the auto-generated path
    _writer.Open(fullSavePath, (int)Math.Round(Image.Width, 0), (int)Math.Round(Image.Height, 0));
}

Key Explanations

  • Path.Combine: This method handles path separators automatically, so you don't have to manually add / or \ between your config path and filename. It works across Windows and other systems seamlessly.
  • Path.ChangeExtension: This guarantees your recording file always uses the .avi format—even if the user types "myVideo" instead of "myVideo.avi".
  • Validation Checks: These small checks prevent your app from crashing if the user leaves the textbox empty or if the config path is invalid. You can tweak the error messages to match your app's UI style.

Quick Reminders

  • Make sure you include using System.IO; at the top of your C# file (it's required for the Path and Directory methods we're using).
  • Move the location variable initialization to your window's constructor or a load event, so it only runs once when your app starts—not every time you hit "record".

内容的提问来源于stack exchange,提问作者btzom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:23:45