iT邦幫忙

0

關於App.config 調路徑

  • 分享至 

  • twitterImage

我的code
例如:
@"D:\DocToImg\111.pptx"

這樣每一次都只能讀到這一個檔案(寫死的這樣,每次別人要用會變成程式還要去改....)

我請教別人,可以把路徑修改寫在App.config上
(可以變成寫成活的這樣...)

可是查了兩天沒什麼頭緒想請教各位大大

如果完成,使用者就能依照他使用狀況,去設定他要開D槽C槽還是E槽了(裡面的ppt檔案,或excel檔了)

看更多先前的討論...收起先前的討論...
神Q超人 iT邦研究生 5 級 ‧ 2018-06-04 16:17:42 檢舉
可以參考看看這裡面的做法
https://dotblogs.com.tw/box5068/2011/02/21/21477
ted8224 iT邦新手 5 級 ‧ 2018-06-04 16:48:03 檢舉
謝謝大大
我馬上看,研究
謝謝您~~
ted8224 iT邦新手 5 級 ‧ 2018-06-05 14:49:39 檢舉
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="111.pptx" value="D:\\DocToImg\\" />

</appSettings>
</configuration>

我的 config 是寫這樣,但我.cs檔 不知道 要寫什麼
我是有把大大給的網址複製貼過去看
不行
不曉的去改哪裡....
神Q超人 iT邦研究生 5 級 ‧ 2018-06-05 16:01:41 檢舉
HI,你用以下的程式碼試試看
//Configuration與AppSettingsSection必須引用System.Configuration才可使用!
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings["111.pptx"].Value = "newPath";
config.Save(ConfigurationSaveMode.Modified);
ted8224 iT邦新手 5 級 ‧ 2018-06-05 16:12:05 檢舉
喔喔 謝謝大大
不過我不是 webconfig 的
我是 Appconfig
謝謝 大大 回答 您可以 看我 最下面 留言
我有把完整code貼給您 參考
ted8224 iT邦新手 5 級 ‧ 2018-06-06 11:43:39 檢舉
大大 請問您,config檔是bin\Debug裡我有找到一個2018_5_30 ALL.exe.config
是改這裡嗎?
那我裡面的
<add key="BBB.pptx" value="D:\\GGo\\" />
這一行 寫這樣
但我去run程式

並不會對 D槽裡GGo資料夾裡的BBB的ppt檔案
去進行我的下面轉檔功能
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
Homura
iT邦高手 1 級 ‧ 2018-06-04 16:19:59
看更多先前的回應...收起先前的回應...
ted8224 iT邦新手 5 級 ‧ 2018-06-04 16:48:24 檢舉

好的
感謝您回復
我來研究看看

ted8224 iT邦新手 5 級 ‧ 2018-06-05 14:49:47 檢舉

我的 config 是寫這樣,但我.cs檔 不知道 要寫什麼
我是有把大大給的網址複製貼過去看
不行
不曉的去改哪裡....

ted8224 iT邦新手 5 級 ‧ 2018-06-05 14:50:12 檢舉
ted8224 iT邦新手 5 級 ‧ 2018-06-05 14:50:55 檢舉
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="111.pptx" value="D:\\DocToImg\\" />

</appSettings>
</configuration>
Homura iT邦高手 1 級 ‧ 2018-06-05 15:07:10 檢舉

你有什麼錯誤嗎?

Homura iT邦高手 1 級 ‧ 2018-06-05 15:13:49 檢舉

ted8224
我剛幫你試了
你要先把System.Configuration加入參考
然後using他
之後應該跟我貼的一樣就沒問題了

ted8224 iT邦新手 5 級 ‧ 2018-06-05 15:51:49 檢舉

有加入了.

using System.Configuration;


namespace _2018_5_30__ALL
{

class Program
    {
        public void writeConfig(string appPath, string key, string value)
        {
            ConfigurationManager.AppSettings["111.pptx"].ToString();
            Configuration config = ConfigurationManager.OpenExeConfiguration(appPath);
            config.AppSettings.Settings[key].Value = value;
            config.Save();
        }

是可以 我只是不曉得 我這樣 寫對嗎?....
上面 有一些 是我從其他地方找來的

接下來 後面 是我一個 轉檔Code功能這樣

Homura iT邦高手 1 級 ‧ 2018-06-05 15:57:49 檢舉

這樣寫沒錯
但是你要修改config?
一般應該手動去改喔

ted8224 iT邦新手 5 級 ‧ 2018-06-05 16:07:06 檢舉

我貼 完整code好了...麻煩 大大 幫看一下...可以run 但是 路徑 怎還是 舊的路徑?

ted8224 iT邦新手 5 級 ‧ 2018-06-05 16:08:30 檢舉

是阿 ...讓 客戶他們去改 路徑就不會變成寫死了...不是嗎?

ted8224 iT邦新手 5 級 ‧ 2018-06-05 16:09:47 檢舉
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.PowerPoint;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Spire.Xls;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Reflection;

namespace _2018_5_30__ALL
{
    class Program
    {
        public void writeConfig(string appPath, string key, string value)
        {
            ConfigurationManager.AppSettings["AAA.pptx"].ToString();
            Configuration config = ConfigurationManager.OpenExeConfiguration(appPath);
            config.AppSettings.Settings[key].Value = value;
            config.Save();
        }
          static void Main(string[] args)
        {
          string pptPath = @"D:\DocToImg\111.pptx"; //ppt檔案路徑
                                                      // string outPutPath = @"D:\DocToImg\+ Interop.PowerPoint-img-{0}.png";  //輸出路徑     
            string outPutPath = @"D:\DocToImg\11.png";  //輸出路徑
            try
            {
                Microsoft.Office.Interop.PowerPoint.Application pptApplication = new Microsoft.Office.Interop.PowerPoint.Application();
                Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Open(pptPath,

Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
                int i = 0;
                //將一頁頁的簡報匯出成圖檔
                foreach (Microsoft.Office.Interop.PowerPoint.Slide pptSlide in pptPresentation.Slides)
                {
                    // string fileName = string.Format("Interop.PowerPoint-img-{0}.png", i);
                    string fileName = string.Format("BB{0}.png", i);

                    outPutPath = @"C:\AAA + Interop.PowerPoint-img-{0}.png";
                    //C:\Users\Egan\Desktop\DocPrinter.Test\DocFolder\ + Interop.PowerPoint-img-{0}.png
                    pptSlide.Export(outPutPath + fileName, "PNG", 800, 600);  //  1024*768 ,調整解析度
                    i++;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
            }
              }
        }

    }

ted8224 iT邦新手 5 級 ‧ 2018-06-05 16:12:20 檢舉
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    
      <add key="111.pptx" value="D:\\GGo\\" />

  </appSettings>
</configuration>

Homura iT邦高手 1 級 ‧ 2018-06-05 16:12:27 檢舉

ted8224
你的問題應該改成讓 客戶他們去改 路徑要如何寫...
這種狀況應該是要用資料庫才對吧...

ted8224 iT邦新手 5 級 ‧ 2018-06-06 11:43:27 檢舉

大大 請問您,config檔是bin\Debug裡我有找到一個2018_5_30 ALL.exe.config
是改這裡嗎?
那我裡面的

這一行 寫這樣
但我去run程式

並不會對 D槽裡GGo資料夾裡的BBB的ppt檔案
去進行我的下面轉檔功能

Homura iT邦高手 1 級 ‧ 2018-06-06 11:49:59 檢舉

對..就是2018_5_30 ALL.exe.config
你用程式修改時是修改到這隻
因為Webform是把原本的檔案複製到bin/Debug底下執行
不會轉檔你可能要下中斷點看一下問題出在哪

我要發表回答

立即登入回答