简体   繁体   中英

AppDomain.CurrentDomain.BaseDirectory path issue when running MVC project

This code creates a log file in App_Data folder. If file doesn't exist it's created. It works when the MVC page is published on my IIS.

 var filename = AppDomain.CurrentDomain.BaseDirectory + 
                "App_Data//" + 
                label + 
                DateTime.Now.ToString() + 
                ".txt";

 var sw = new System.IO.StreamWriter(filename, true);

However when i run the project local, i get :

NotSupportedException. the StreamWriter says that my path is not supported. This is the path im getting:

"C:\\Projects\\x- chefsportalen 2.0\\Chefsportalen\\App_Data//Paragå manuell2015-05-18 11:00:18.txt"

The App_Data folder exist and the path is correct, why am i getting this error?

Filenames are not allowed to contain the ":" character.

Use an explicit format string to specify a format that doesn't contain ":", eg

DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss", CultureInfo.InvariantCulture)

I'm not sure why it's working when you publish to IIS - maybe because IIS is using a culture that doesn't have ":" as time separator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM