在 Delphi 中更改路径

Changing paths in Delphi

我正在做一个项目,我需要从 Delphi 中的文件播放视频。我经常在家和学校工作,但我遇到的问题是,在家里,我的 USB 是驱动器 'J',而在学校,我的 USB 是驱动器 'D'.

我每次都手动去改。 Delphi 有没有办法从任何地方自动获取视频?

每个扇区上都有一个图像组件,用于选择扇区。

*注意,我知道我可以在 Delphi 中搜索特定文件的位置,但我有超过 24 个不同的地方需要播放不同的视频,所以搜索可能是我最后的选择,除非我使用一个程序并为每个扇区设置常量以区分它们。

目前的代码如下所示:

procedure TtForm.imgSector1Click(Sender: TObject);
begin
  
    //Variables,this is for initializing them when I create them later.                                                                                                                                             

  //Procedures                                                                                                   
      SectorDeselect; //Procedure to turn all sector borders white                                            

  // Video                                                                                                       
  WindowsMediaPlayer1.Controls.stop;                                                                        
  WindowsMediaPlayer1.URL := 'J:\IT\PAT\phase 2\Videos\Footage1.mp4'; //Where my problem lies             
  WindowsMediaPlayer1.Controls.Play;

  // Sector Info. The memos and Rich edits                                                                                                                                                                       
  redSectorInfo.Lines.Clear;                                                                                                                                                                              
  redSectorInfo.Lines.Add('');                                                                                                                                                             

  // Sector. Highlighting the sector borders surrounding the sector                                                                                                   
  SectorBordr1.Brush.Color := clGreen;                                                                          
  SectorBorder10.Brush.Color := clGreen;                                                                                                                                                                                                               
end;

我建议在您的应用程序 UI 中添加一个 TEdit 控件,以便您可以为该应用程序当前 运行 正在运行的计算机上的文件指定基础 drive/path。然后,您的代码可以在 运行 时构建相对于该基本路径的单个文件路径。不要在代码中使用硬编码路径。

然后您可以将该基本路径保存到 Windows 注册表中您创建的新密钥中,即 HKEY_CURRENT_USER\Software\MyApp。或者,您可以将路径保存在 Windows 用户个人资料的子文件夹中创建的配置文件(INI、XML、JSON 等)中,例如 %APPDATA%\MyApp。每次应用程序 运行.

时,您的代码都可以读取该基本路径

如果文件存储在 USB 驱动器上,另一种解决方案是在 运行 时简单地枚举可用驱动器,例如 GetLogicalDriveStrings(). For each drive, append a relative path for a given file onto the end of it, and then check if that file exists, such as with FileExists()。如果是这样,您现在知道在下一次您的应用程序 运行 之前使用哪个驱动器来存储所有文件(您可以保存 运行 之间的驱动器路径,如上所述)。如果找不到该文件,请转到下一个驱动器。

在 CommandLine 上添加一个参数怎么样?

开始

D:\myfolder\myfileD

开始

J:\myfolder\myfileJ

GUI 文件可以接受参数。使用如下代码捕获它:

DriveLetter := ParamStr(1);