gpt4 book ai didi

electron - 在 Electron 中写入 "appData"中的文件。在哪里添加 import { app } from "electron";?

转载 作者:行者123 更新时间:2023-12-03 12:30:04 32 4
gpt4 key购买 nike

我正在制作我的第一个 Electron 应用程序。我正在尝试将文本文件保存到 appData 文件夹(例如 C:\Users\user\AppData\Roaming)。我知道我需要添加 从“Electron ”导入{应用}; 有些地方,但我不确定把它放在哪里。

在我的 index.js javascript 中,我正在编写用户以他的表单提交到文本文件的数据库设置。这是我需要有 appData 目录地址的地方。

// Write data to text file
var filepath = app.getPath("appData")
var filename = "database_quick_image_forensics.txt"
var inp_data = inp_host + "|" + inp_username + "|" + inp_password + "|" + inp_database_name + "|" + inp_table_prefix;
write_to_file(filepath, filename, inp_data);

我的整个代码如下:

./setup/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Setup</title>
<!-- https://electronjs.org/docs/tutorial/security#csp-meta-tag -->

<!-- CSS -->
<link rel="stylesheet" type="text/css" href="../_webdesign/dark/dark.css" />
<!-- // CSS -->


<!-- jQuery -->
<script>window.$ = window.jQuery = require('../javascripts/jquery/jquery-3.4.1.js');</script>
<script src="../javascripts/jquery/jquery-3.4.1.js" charset="utf-8"></script>
<!-- //jQuery -->

<!-- jQuery -->
<script src="./index.js" charset="utf-8"></script>
<!-- //jQuery -->
</head>
<body>
<div id="main_single_column">
<h1>Setup</h1>

<!-- Feedback -->
<div id="feedback_div" class="success">
<p id="feedback_p">Success</p>
</div>
<!-- //Feedback -->

<!-- Database connection form -->

<p>Host:<br />
<input type="text" name="inp_host" id="inp_host" value="localhost" />
</p>

<p>Port:<br />
<input type="text" name="inpport" id="inp_port" value="" />
</p>

<p>Username:<br />
<input type="text" name="inp_username" id="inp_username" value="root" />
</p>

<p>Password:<br />
<input type="text" name="inp_password" id="inp_password" />
</p>

<p>Database name:<br />
<input type="text" name="inp_database_name" id="inp_database_name" value="quick" />
</p>

<p>Table prefix:<br />
<input type="text" name="inp_table_prefix" id="inp_table_prefix" value="cf_" />
</p>

<p>
<button id="form_connect_to_database_submit">Connect to database</button>
</p>

<!-- //Database connection form -->
</div>



</body>
</html>

./setup/index.js
const fs = require('fs');

// Action = On submit
$(document).ready(function(){

$("#form_connect_to_database_submit").click( function() {
// Feedback
$('#feedback_div').show();
$('#feedback_div').removeClass("success");
$('#feedback_div').addClass("info");
$('#feedback_p').text("Connecting!")

// get all the inputs
var inp_host = $("#inp_host"). val();
var inp_username = $("#inp_username"). val();
var inp_password = $("#inp_password"). val();
var inp_database_name = $("#inp_database_name"). val();
var inp_table_prefix = $("#inp_table_prefix"). val();

// Test connection
var connection_result = connect_to_database(inp_host, inp_username, inp_password, inp_database_name, inp_table_prefix);
if(connection_result != "connection_ok"){
// Connection Failed
$('#feedback_div').removeClass("info");
$('#feedback_div').addClass("error");
$('#feedback_p').text(connection_result)
}
else{
// Connection OK
$('#feedback_div').removeClass("info");
$('#feedback_div').addClass("success");
$('#feedback_p').text("Connected")

// Write data to text file
var filepath = app.getPath("appData")
var filename = "database_quick_image_forensics.txt"
var inp_data = inp_host + "|" + inp_username + "|" + inp_password + "|" + inp_database_name + "|" + inp_table_prefix;
$('#feedback_p').text("Connected " + filepath)
write_to_file(filepath, filename, inp_data);

// Feedback
$('#feedback_div').removeClass("info");
$('#feedback_div').addClass("success");
$('#feedback_p').text("Connected to")
}




});
$('#inp_host').focus();
});


// Function connect to database
function connect_to_database(inp_host, inp_username, inp_password, inp_database_name, inp_table_prefix){
var mysql = require('mysql');

// Add the credentials to access your database
var connection = mysql.createConnection({
host : inp_host,
user : inp_username,
password : null, // or the original password : 'apaswword'
database : inp_database_name
});

// connect to mysql
connection.connect(function(err) {
// in case of error
if(err){
console.log(err.code);
console.log(err.fatal);
return err.code;
}
});


// Perform a query
$query = 'SELECT * FROM `cf_admin_liquidbase` LIMIT 10';
connection.query($query, function(err, rows, fields) {
if(err){
console.log("An error ocurred performing the query.");
console.log(err);
return;
}
console.log("Query succesfully executed", rows);
});

return "connection_ok";
} // connect_to_database




// Function write setup
function write_to_file(filepath, filename, inp_data){


var fullpath = filepath + "\\" + filename;
fs.writeFile(fullpath, inp_data, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
console.log('Lyric saved!');
});
} // write_to_file

./main.js
const { app, BrowserWindow } = require('electron')


// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win


function createWindow () {
// Create the browser window.
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})

// and load the index.html of the app.
win.loadFile('index.html')

// Open the DevTools.
// win.webContents.openDevTools()

// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

最佳答案

I know I need to add import { app } from "electron"; some where but I am unsure where to place it.



app模块总是(根据我的经验)导入你的 main过程,以便您可以控制应用程序的生命周期。但是,如果你想使用一些 app renderer 中的模块功能进程,您可以通过 remote 将其导入那里模块(如该问题的已接受答案所示: How to use electron's app.getPath() to store data?)
const remote = require('electron').remote;
const app = remote.app;
console.log(app.getPath('userData'));
mainrenderer过程是 Electron 中的关键概念所以我建议阅读这些。要点是你有一个 main过程——它没有可视化表示,它与您的应用程序的生命周期有关,创建和销毁 renderer进程(如 BrowserWindows ),渲染器进程之间的通信等 - 你可以拥有尽可能多的 renderer根据需要处理。

所以如果你想读写文件,你可以在 renderer 中进行。过程如上图 - 或者您可以在 main 中进行过程。在后一种情况下,如果 renderer进程要保存文件,它可以向 main 发送消息通过 IPC 处理,发送要保存的数据。

采用哪种方式是一种架构选择。

关于electron - 在 Electron 中写入 "appData"中的文件。在哪里添加 import { app } from "electron";?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59724062/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com