为什么相对路径在NWJS应用程序中不起作用?

发布于 2025-02-10 12:54:04 字数 1133 浏览 2 评论 0原文

只有绝对路径才能起作用。此代码有效。如果我使用../../../ system.user/documents/documents.nfo'它将无法使用。

编辑:如何使用/%application%/system.user/documents/documents.nfo之类的变量?这样可以吗?

main.js

function OpenDocuments() {
    const path = require('path');
    nw.Shell.showItemInFolder(path.resolve('D:/PROTON DRIVE/PROTON.USER/DOCUMENTS/DOCUMENTS.NFO'));
}

default.html

<div name="FolderMenuContainer" id="FolderMenuContainer" class="FolderMenuContainer">
    <span name="FolderIcon" id="FolderIcon" class="material-icons"> description </span>
    <span name="FolderText" id="FolderText" class="FolderText" onclick="OpenDocuments()"> Documents </span>
</div>

文件结构

“

Only absolute paths are working. This code works. If I use ../../../SYSTEM.USER/DOCUMENTS/DOCUMENTS.NFO' it won't work.

EDIT: How can I use a variable like /%APPLICATION%/SYSTEM.USER/DOCUMENTS/DOCUMENTS.NFO? Is that possible in this manner?

Main.js

function OpenDocuments() {
    const path = require('path');
    nw.Shell.showItemInFolder(path.resolve('D:/PROTON DRIVE/PROTON.USER/DOCUMENTS/DOCUMENTS.NFO'));
}

Default.html

<div name="FolderMenuContainer" id="FolderMenuContainer" class="FolderMenuContainer">
    <span name="FolderIcon" id="FolderIcon" class="material-icons"> description </span>
    <span name="FolderText" id="FolderText" class="FolderText" onclick="OpenDocuments()"> Documents </span>
</div>

File Structure

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾听心声的旋律 2025-02-17 12:54:04

我使用的一种方法是使用path.joinprocess.cwd()CWD是指“当前工作目录”。

示例:

const path = require('path');
const place = path.join(process.cwd(), '..', '..', 'my-folder');

您还可以使用dirname,该相对于文件执行的文件,这可能与当前的工作目录不同。

const path = require('path');
const place = path.join(__dirname, '..', '..', 'my-folder');

如果您想使用解决方案Windows环境变量,我建议您制作这样的辅助功能:

/**
 * Replaces all environment variables with their actual value.
 * Keeps intact non-environment variables using '%'.
 *
 * @example
 * // 'C:\Users\bob\Desktop\AMD64'
 * resolveWindowsEnvironmentVariables('C:\Users\%USERNAME%\Desktop\%PROCESSOR_ARCHITECTURE%');
 *
 * @param  {string} filePath  The input file path with percents.
 * @return {string}           The resolved file path.
 */
function resolveWindowsEnvironmentVariables (filePath) {
  if (process.platform !== 'win32') {
    return filePath;
  }
  if (!filePath || typeof(filePath) !== 'string') {
    return undefined;
  }

  /**
   * Returns the value stored in the process.env for a given
   * environment variable. Or the original '%ASDF%' string if
   * not found.
   *
   * @example
   * replaceEnvironmentVariable('%USERNAME%', 'USERNAME');
   *
   * @param  {string} withPercents     '%USERNAME%'
   * @param  {string} withoutPercents  'USERNAME'
   * @return {string}                  'bob' || '%USERNAME%'
   */
  function replaceEnvironmentVariable (withPercents, withoutPercents) {
    let found = process.env[withoutPercents];
    // 'C:\Users\%USERNAME%\Desktop\%asdf%' => 'C:\Users\bob\Desktop\%asdf%'
    return found || withPercents;
  }

  // 'C:\Users\%USERNAME%\Desktop\%PROCESSOR_ARCHITECTURE%' => 'C:\Users\bob\Desktop\AMD64'
  filePath = filePath.replace(/%([^%]+)%/g, replaceEnvironmentVariable);

  return filePath;
}

您可能还需要解决非窗口路径

/**
 * Resolves paths that start with a tilde to the user's
 * home directory.
 *
 * @example
 * // '/home/bob/GitHub/Repo/file.png'
 * resolveTilde('~/GitHub/Repo/file.png');
 *
 * @param  {string} filePath  '~/GitHub/Repo/file.png'
 * @return {string}           '/home/bob/GitHub/Repo/file.png'
 */
function resolveTilde (filePath) {
  if (process.platform === 'win32') {
    return filePath;
  }
  if (!filePath || typeof(filePath) !== 'string') {
    return undefined;
  }

  const os = require('os');
  // '~/folder/path' or '~' not '~alias/folder/path'
  if (filePath.startsWith('~/') || filePath === '~') {
    return filePath.replace('~', os.homedir());
  }

  return filePath;
}

One way I use is to use path.join and process.cwd(). cwd means "Current Working Directory".

Example:

const path = require('path');
const place = path.join(process.cwd(), '..', '..', 'my-folder');

You can also use __dirname which is relative to the file the code is executed from, which may be different from the Current Working Directory.

const path = require('path');
const place = path.join(__dirname, '..', '..', 'my-folder');

If you want to use resolve Windows environment variables, I would recommend making a helper function like this:

/**
 * Replaces all environment variables with their actual value.
 * Keeps intact non-environment variables using '%'.
 *
 * @example
 * // 'C:\Users\bob\Desktop\AMD64'
 * resolveWindowsEnvironmentVariables('C:\Users\%USERNAME%\Desktop\%PROCESSOR_ARCHITECTURE%');
 *
 * @param  {string} filePath  The input file path with percents.
 * @return {string}           The resolved file path.
 */
function resolveWindowsEnvironmentVariables (filePath) {
  if (process.platform !== 'win32') {
    return filePath;
  }
  if (!filePath || typeof(filePath) !== 'string') {
    return undefined;
  }

  /**
   * Returns the value stored in the process.env for a given
   * environment variable. Or the original '%ASDF%' string if
   * not found.
   *
   * @example
   * replaceEnvironmentVariable('%USERNAME%', 'USERNAME');
   *
   * @param  {string} withPercents     '%USERNAME%'
   * @param  {string} withoutPercents  'USERNAME'
   * @return {string}                  'bob' || '%USERNAME%'
   */
  function replaceEnvironmentVariable (withPercents, withoutPercents) {
    let found = process.env[withoutPercents];
    // 'C:\Users\%USERNAME%\Desktop\%asdf%' => 'C:\Users\bob\Desktop\%asdf%'
    return found || withPercents;
  }

  // 'C:\Users\%USERNAME%\Desktop\%PROCESSOR_ARCHITECTURE%' => 'C:\Users\bob\Desktop\AMD64'
  filePath = filePath.replace(/%([^%]+)%/g, replaceEnvironmentVariable);

  return filePath;
}

You may also want to resolve non-windows paths

/**
 * Resolves paths that start with a tilde to the user's
 * home directory.
 *
 * @example
 * // '/home/bob/GitHub/Repo/file.png'
 * resolveTilde('~/GitHub/Repo/file.png');
 *
 * @param  {string} filePath  '~/GitHub/Repo/file.png'
 * @return {string}           '/home/bob/GitHub/Repo/file.png'
 */
function resolveTilde (filePath) {
  if (process.platform === 'win32') {
    return filePath;
  }
  if (!filePath || typeof(filePath) !== 'string') {
    return undefined;
  }

  const os = require('os');
  // '~/folder/path' or '~' not '~alias/folder/path'
  if (filePath.startsWith('~/') || filePath === '~') {
    return filePath.replace('~', os.homedir());
  }

  return filePath;
}
音盲 2025-02-17 12:54:04

由于Jaredcheeda,能够弄清楚了!解决我问题的具体解决方案是:

function RunNotePad() {
    const path = require('path');
    nw.Shell.openItem(path.join(process.cwd(), '..', '..', '..', 'PROTON.FILES/PROGRAM.FILES/V8.4.2_NotePad++', 'notepad++.exe'));
}

每个'..'向后移动一个目录。我尝试在我的原始代码中使用../../../在我的文件路径中使用,但它不起作用。使用方法,杰罗德解释了它正在起作用!

Was able to figure it out thanks to Jaredcheeda! The specific solution to my problem is this:

function RunNotePad() {
    const path = require('path');
    nw.Shell.openItem(path.join(process.cwd(), '..', '..', '..', 'PROTON.FILES/PROGRAM.FILES/V8.4.2_NotePad++', 'notepad++.exe'));
}

Each '..' moves backwards a directory. I tried using the ../../../ in my file path in my original code and it wasn't working. Using the method Jerod explained it is working!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文